PDA

View Full Version : replacetext() and striptext()


Tigairius
04-26-2008, 09:51 AM
replacetext and striptext


function replacetext(text, oldtext, newtext) {
temp.oldlen = oldtext.length();
temp.textdiff = newtext.length() - temp.oldlen;

for (temp.p: text.positions(oldtext)) {
temp.pos = temp.p + temp.textdiff * (temp.index ++);
text = text.substring(0, temp.pos) @ newtext @ text.substring(temp.pos + temp.oldlen);
}

return text;
}

function striptext(text, oldtext) {
return replacetext(text, oldtext, "");
}

cbk1994
04-26-2008, 07:31 PM
I've needed something like this many times, and I've built my own.

This will save me a lot of time, thanks so much!

rep++ sometime within the next 24 hours, unless I've given too much rep to you. We shall see.

zokemon
04-29-2008, 03:46 AM
You could also create a GuiMLTextCtrl, set the text variable to the string with the \n's then just pull out getlines() which will return the lines as an array.

Another option is to savestring() to a temp file, then loadlines(). Both of these are kind of unnecessary though but should be mentioned regardless.

EDIT:
I don't get why you didn't just do like:
function replacetext(text) {
return(text.tokenize("\n"));
}

Tigairius
04-29-2008, 04:39 AM
You could also create a GuiMLTextCtrl, set the text variable to the string with the \n's then just pull out getlines() which will return the lines as an array.

Another option is to savestring() to a temp file, then loadlines(). Both of these are kind of unnecessary though but should be mentioned regardless.

EDIT:
I don't get why you didn't just do like:
function replacetext(text) {
return(text.tokenize("\n"));
}

Well simply tokenizing it caused quite a few problems.

Tigairius
12-06-2012, 08:36 AM
Recently needed replacetext again and didn't like what I had in here. So I updated my original post with a couple of changes. The function runs a little faster than the others on the code gallery at the moment too.