Author Topic: Replacing A String Up To A Point [Solved]  (Read 392 times)

Is there any way I can replace a string up until it reaches a ' ?
« Last Edit: March 06, 2012, 01:48:42 PM by jes00 »

Code: [Select]
function strReplaceUntil( %string, %search, %replace, %until )
{
%pos = strPos( %string, %until );

if ( %pos < 0 )
{
%pos = strLen( %string );
}

return strReplace( getSubStr( %string, 0, %pos ), %search, %replace ) @ getSubStr( %string, %pos + strLen( %until ), strLen( %string ) );
}
« Last Edit: March 05, 2012, 06:44:44 AM by Port »

Port's is good, assuming that you want to search for something up until a point.

If you want to replace it no matter what's there, here is the code you should use:

Code: [Select]
function replaceUpTo(%String, %UpToChar, %Replacement, %AlwaysReplace)
{
//If %AlwaysReplace is false and %UpToChar is not found, then return.
//Otherwise, return the replacement.
%Pos = strPos(%String, %UpToChar);
if(%Pos <= 0 && !%AlwaysReplace)
return %String;
else if(%Pos <= 0 && !!%AlwaysReplace)
return %Replacement;
return %Replacement @ getSubStr(%String, %Pos + 1, strLen(%String);
}

Code: [Select]
function strReplaceUntil( %string, %search, %replace, %until )
{
%pos = strPos( %string, %until );

if ( %pos < 0 )
{
%pos = strLen( %string );
}

return strReplace( getSubStr( %string, 0, %pos ), %search, %replace ) @ getSubStr( %string, %pos + strLen( %until ), strLen( %string ) );
}
Is %until a number?

EDIT: Talked to Port on his server, he solved it.
« Last Edit: March 06, 2012, 01:48:31 PM by jes00 »