Blockland Forums > Modification Help
Is there a way to StrReplace() more then once in a string AND set a value?
Pages: (1/1)
adam savage:
I need to fix a string where "youre" becomes "you're" and "cant" becomes "can't" and use %this.Setvalue() to input the strings.
Chrono:
%str = %this.getValue();
%str = strReplace(%str,"youre","you're");
%str = strReplace(%str,"cant","can't");
%this.setValue(%str);
Ipquarx:
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: ---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));
}
--- End code ---
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!
Port:
--- Quote from: Ipquarx on March 10, 2012, 02:02:25 PM ---
--- Code: ---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;
}
--- End code ---
--- End quote ---
Fixed.
Pages: (1/1)