Coyote Murphy
Beelphazoaric
Join date: 12 Aug 2003
Posts: 91
|
09-08-2003 19:30
Tossing this out for public consumption.... I'm more than open to simpler ways of doing this, if you know of them, I'm sure most of you serious scripters out there have a favorite way of parsing strings. Overcommented on purpose! // freeSoftCode :: origin:Coyote Murphy
string StringReplace( string src, // the string to be operated upon string find, // the character(s) to be found string replace) // the character(s) substituted // (can be "" for deletion) { string remain = src; // text left to be parsed string msg = ""; // text already parsed
integer loc = llSubStringIndex(remain,find); // location of the first example of find integer findLen = llStringLength(find); integer remainLen = llStringLength(remain);
string before; string after;
if (loc == -1) // 'find' was not in 'src', end { return src; // send back untouched string. }
while(loc != -1) // as long as 'find' is in 'remain'... { if (loc > 0) { before = llGetSubString(remain,0,loc - 1); // copy left side of string before 'find', // if 'find' isn't at start of 'remain' } if ((loc + findLen) <= (remainLen - 1)) { after = llGetSubString(remain, loc + findLen, remainLen - 1); // copy right side of string after 'find', // if 'find' isn't at end of 'remain' } msg = msg + before + replace; // already-parsed string is stored in 'msg'
remain = after; // remainder (unparsed) saved in 'remain'
// vars reset for next while() loop: remainLen = llStringLength(remain); loc = llSubStringIndex(remain,find); before = ""; after = ""; }
// all 'find' occurances have been replaced return msg; }
|
Ama Omega
Lost Wanderer
Join date: 11 Dec 2002
Posts: 1,770
|
09-08-2003 22:41
This one is a little smaller  : integer findLength; integer space;
string replace(string src, string find, string rep) { findLength = llStringLength(find); space = llSubStringIndex(src,find);
while (space != -1) { src = llGetSubString(src,space + findLength,space - 1); src = llInsertString(src,space,rep); space = llSubStringIndex(src,find); } return src; } Be a little wary though, if you only want to find the word cat for instance and not the first three letters of catastrophy you should include spaces in your search ie " cat " instead of "cat". And the use is: replace(source,find,replaceWith)
|
Coyote Murphy
Beelphazoaric
Join date: 12 Aug 2003
Posts: 91
|
09-09-2003 20:40
Ama, yours isn't going to handle a replace string that contains the find string without going into an endless loop: replace("abba","a","ab"  1. replaces first 'a' with 'ab', abbba 2. replaces first 'a' with 'ab', abbbba 3. replaces first 'a' with 'ab', abbbbba n. replaces first... well, you get the idea. Most of my function's unfortunate length was just to deal with this rare yet inevitable sort of call.
|
Coyote Murphy
Beelphazoaric
Join date: 12 Aug 2003
Posts: 91
|
09-10-2003 07:11
Here's a revision, tightened up a little using some of Ama's enhancements (like using llInsertString, and using llGetSubString's character exclusion aspect). This version will also handle a situation where the find string also occurs in the replace string. string StringReplace(string src,string find,string replace) { integer loc = llSubStringIndex(src,find); integer findLen = llStringLength(find); integer replaceLen = llStringLength(replace); while (loc != -1) { src = llInsertString(llGetSubString(src,loc + findLength,loc -1),loc,replace); loc = llSubStringIndex(llGetSubString(src,loc + replaceLen,llStringLength(src) - 1),find) + replaceLen; if (loc < replaceLen) { loc = -1; } } return src; }
(This is untested code -- no access to SL at work. I'm pretty confident it'll work, though.)
|