string llInsertString(string dst, integer position, string src)
Inserts
src into
dst at
position and
returns the result.
Unlike most other string/
list functions, this one does not support using a negative position to count backwards from the end of the string.
Example:
ARGH!!! What an evil little ambiguity! I kept fussing with this function, trying to get it to count backwards... what a waste of time.
Channeling this anger, I created this little code snippet that will provide the backwards counting: -Chris
string insertString(string destination, integer position, string str) {
if (position < 0)
position += llStringLength(destination) + 1;
return llInsertString(destination, position, str);
}
Passing
-1 as
position will provide the same functionality as appending
str to the end of
destination with the += operator.
I don't think passing -1 should append to the end, if this behaivor is wanted a very big integer should be passed instead. Using -1 this way, makes negative indexes work differently with this function then the other LSL functions. Using large integers (like 0x7FFFFFFF) has not been shown to effect performance -
BW
string insertString(string destination, integer position, string str) {
if (position < 0)
position += llStringLength(destination);
return llInsertString(destination, position, str);
}
str_replace by Chad Statosky -
The list method can be nasty.
str_replace entirely rewriten by
BW to be more robust.
string str_replace(string from, string to, string str)
{
integer e = llStringLength(from) - 1;
if(e > -1)
{
string b = str;
integer c = -1;
integer f = llStringLength(to) - 1;
@loop;//instead of a while loop, saves 5 bytes (and run faster).
integer d = llSubStringIndex(b, from) + 1;
if(d)
{
// c+=d;
// str = llInsertString(llDeleteSubString(str,c,c+e), c, to);
// c += f;
// b = llGetSubString(str, c + 1, 0x4000);
b = llGetSubString(str = llInsertString(llDeleteSubString(str,c+=d,c+e), c, to), (c += f) + 1, 0x4000);
jump loop;
}
}
return str;
}
Functions |
String