[FMX] indexOf replace words

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?

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, or do your parsing in a server side language!

This is exactly what I was looking for! :slight_smile: =)
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.

niiiiiice (-: