View Full Version : [FMX] indexOf replace words
andr.in
11-17-2002, 08:46 AM
Input field: "input"
if (input.indexOf("word") == 0) {
something...
}
that detects if there is a word "word" written in the input textbox.
But is there a way... how can I detect the word and then replace it with *
like this: ****
I just said a bad word... and it was replaced with **** how can I do that?
sbeener
11-18-2002, 03:46 PM
here's a handy prototype i had fun putting together:
String.prototype.replace = function(find,replace){
var n,r,str = this;
if(typeof find == "string") find = [find];
if(typeof replace == "string") replace = [replace];
n = find.length;
while(n--){
if(str.indexOf(find[n]) > -1){
r = (replace[n] != undefined) ? replace[n] : replace[replace.length-1];
str = str.split(find[n]).join(r);
};
};
return str;
};
input = "Ah, le con! Il me fait vraiment chier!";
search = ["con","merde","chier"];
replace = ["{one}","{two}","{three}"];
replacedInput = input.replace(search,replace);
// returns "Ah, le {one}! Il me fait vraiment {three}!"
this prototype returns a string with the search terms replaced by the replace terms. note that this function is case sensitive.
either search or replace can be an array. if both are arrays, each search term is replaced with it's matching replace term ( ie. all instances of search[2] would be replaced with replace[2], search[3] with replace[3], etc.). if the search array is longer than the replace array, the last replace item is used.
if search is an array and replace is a string, all instances are replaced with the replace string.
if you want more powerful features, i recommend pavils jurjans's RegExp class (http://www.jurjans.lv/flash/RegExp.html), or do your parsing in a server side language!
andr.in
11-18-2002, 04:00 PM
This is exactly what I was looking for! :) =)
thanx man!
I was helping someone who tried to remove characters from a string, and I used this tutorial. I just thought I'd share, even though it's very close to this prototype://Credits for the prototype: Sbeener
a="This is Cool! Isn't it cool?";
thingsToRemove=["!","?"];
String.prototype.removeChar=function(find){
var n,str=this;
if(typeof find == "string") find = [find];
n=find.length;
while(n--){
if(str.indexOf(find[n]) > -1){
str = str.split(find[n]).join("");
};
};
return str;
};
trace (a.removeChar(thingsToRemove));
// returns "This is cool Isn't it cool"And I must say that the split.join trick is lovely :beam: I would never have thought of that.
lostinbeta
01-15-2003, 10:01 PM
niiiiiice (-:
Could this also be used somehow to truncate text that is too long and replace the rest of it with an ellipsis?
for example:
"Giant Monkeys Eat Toronto" becomes "Giant Monkeys Eat T..."
Any advice? :hat:
Thanks!
lrhb
lostinbeta
10-13-2003, 06:57 PM
Try this...
String.prototype.truncate = function(chars) {
return this.length>chars ? this.slice(0, chars-3)+"..." : null;
};
myString = "Giant Monkeys Eat Toronto";
trace("Normal Version: "+myString);
trace("Truncated Version: "+myString.truncate(22));
That will truncate if the text is more than 22 characters long (it will chop it down to 19 characters and add 3 dots, making 22 characters in total after truncation)
Dizzamn, lostinbeta!
Three words: SLICKER. THAN. GREASE!
One more question though... for my information that is less than 22 characters, it comes out as null... is that the workings of the " "..." : null;" part of the prototype?
Thanks though, a real slick solution!
lrhb :hat:
lostinbeta
10-13-2003, 08:00 PM
LOL!!! OOOOPS!!!! ::blushes::
replace "null" with "this". String.prototype.truncate = function(chars) {
return this.length>chars ? this.slice(0, chars-3)+"..." : this;
};
And this should help makes sense of the ?: thing...
http://www.kirupa.com/developer/actionscript/tricks/tertiary.htm
hah hah! That would explain it! :D
Thanks again!!!
lrhb
lostinbeta
10-13-2003, 08:04 PM
No problem lrhb... I actually have never even thought of doing something like this before, so it kinda inspired me to figure it out... so now this little prototype goes to my list of useful prototypes :)
awe! Good! I'm glad I can be of some service instead of being such a nooge, asking so many questions! :hat:
lostinbeta
10-13-2003, 08:12 PM
What's a Flash forum for if you can't ask questions about Flash? ;)
tbarjoe
02-23-2004, 08:19 PM
sbeener - Thanks a lot for that code, pretty much exactly what I was looking for as well, but I wanted a case Insensitive version, so here is what I changed. I am new to Actionscript, so I know this is an ugly solution (ewww... nested loops).
var n,loc,swearUpper,strUpper = this.toUpperCase(),str = this;
if(typeof find == "string") find = [find];
if(typeof replace == "string") replace = [replace];
n = find.length;
strArray = str.split("");
while(n--){
swearUpper = find[n].toUpperCase();
loc = strUpper.indexOf(swearUpper);
len = find[n].length
while( loc > -1){
for(var i=0;i<len;i++){
strArray.splice(loc+i,1,replace[n].charAt(i));
}
loc = strUpper.indexOf(swearUpper);
strUpper = strArray.join("").toUpperCase();
};
};
strArray = strArray.join("");
return strArray;
I know that it removes the capitalization when it replaces it, but that is acceptable (I'm just filtering for bad words on an instant messenger)
Thanks a lot everyone, I don't post much here, but there is a lot of really good stuff.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.