Author Topic: Is there a way to StrReplace() more then once in a string AND set a value?  (Read 534 times)

I need to fix a string where "youre" becomes "you're" and "cant" becomes "can't" and use %this.Setvalue() to input the strings.

%str = %this.getValue();
%str = strReplace(%str,"youre","you're");
%str = strReplace(%str,"cant","can't");
%this.setValue(%str);

I was so bored, I made a handler for easily adding words to replace.
And I say words because if the string is "youreyoureyoureyoure" then it'l become "you'reyou'reyou'reyou're", and I think replacing just the words seems more appropriate for this purpose.

Note that I don't have a return in here because if there is no return in a function and the last operation is setting a variable, it automatically returns that variable.
Code: [Select]
function MultipleReplace(%String, %WordsFrom, %WordsTo)
{
if(%String $= "" || (%WordsFrom = stripTrailingSpaces(%WordsFrom)) $= "" || (%WordsTo = stripTrailingSpaces(%WordsTo)) $= "")
return %String;
else if((%FromC = getWordCount(%WordsFrom)) < (%ToC = getWordCount(%WordsTo)))
%WordsTo = getWords(%WordsTo, 0, %FromC - %ToC);
for(%a = 0; %a < %FromC; %a++)
%String = strReplace(%String, getWord(%WordsFrom, %a), getWord(%WordsTo, %a));
}
Usage:
MultipleReplace(String, Words to replace seperated by spaces, What to replace them with seperated by Spaces);

Example:

MultipleReplace("Hello there World!", "Hello World!", "Bonjour Ipquarx!");
Will return Bonjour there Ipquarx!

Code: [Select]
function MultipleReplace(%String, %WordsFrom, %WordsTo)
{
if(%String $= "" || (%WordsFrom = stripTrailingSpaces(%WordsFrom)) $= "" || (%WordsTo = stripTrailingSpaces(%WordsTo)) $= "")
return %String;
else if((%FromC = getWordCount(%WordsFrom)) < (%ToC = getWordCount(%WordsTo)))
%WordsTo = getWords(%WordsTo, 0, %FromC - %ToC);
for(%a = 0; %a < %FromC; %a++)
%String = strReplace(%String, getWord(%WordsFrom, %a), getWord(%WordsTo, %a));
return %String;
}

Fixed.