Author Topic: Replacing 1 word in a string?  (Read 733 times)

Yeah so how would I, for example, replace the first word only of a string.

strReplace replaces all cases of the thing you're replacing.

Also how do you replace only words? Say I wanted to replace all I's if I done strReplace it would replace all I's even if they're in the middle of a word.
« Last Edit: November 22, 2007, 02:27:34 PM by MrPickle »

I'm not very good at scripting, but for the I's, make it replace {space here}I{space here}. Maybe? Probably not.

Code: [Select]
%t = getword(%text,0);
%t = strreplace(%t,"5");
setword(%text,0,%t);

Code: [Select]
%text = strReplace(%text," I ");

something like that.
I'll edit or post again when I find the way to put %t back into %text.
edit: found
« Last Edit: November 22, 2007, 02:38:10 PM by rkynick »

Do you always want to change the first word, or only if it is equal to something?
Code: [Select]
%line = "This is a sentence";
%first = firstWord(%line);
%rest = restWords(%line);
// Do whatever to %first
%newline = %first SPC %rest;

To replace only words:
Code: [Select]
function ReplaceWords(%string, %find, %replace){
for(%i=0;%i<getWordCount(%string);%i++){
%word = getWord(%string, %i);
%add = %word;
if(%word $= %find)
%add = %replace;
%return = %i == 0 ? %add : %return SPC %add;
}
return %return;
}