A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: split indexOf?

  1. #1
    Senior Member
    Join Date
    Jul 2010
    Posts
    111

    split indexOf?

    Hi, this might sound kind of noobish, just wondering if you can do something like:

    mytext=mytext.split(mytext.indexOf("cat")).join("d og")

    Because if I had a sentence:

    I love to pet my cat. My cat is so nice.

    I would only want to replace the first "cat", not the second. Split/join will replace both. Just wondering if there's a way. (p.s. actionscript 2, so cant do "replace" I dont think)

    Thanks!

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    No, but how about creating your own function ;D?

    Actionscript Code:
    myText = "I love to pet my cat. My cat is so nice.";

    String.prototype.replaceWord = function(word, new_word, index){
        indexes = new Array();
        newSentence = this.split("");
        newWord = word.split("");
        for(i=0;i<newSentence.length;i++){
            if(newSentence[i] == newWord[0]){
                checkLength = 0;
                for(a=0;a<newWord.length;a++){
                    if(newSentence[i+a] == newWord[a]){
                        checkLength++;
                    }
                }
                if(checkLength == newWord.length){
                    indexes.push(i);
                }
            }
        }
        if(indexes.length > 0){
            return this.substring(0, indexes[index])+new_word+this.substring(indexes[index]+word.length, this.length);
        } else {
            trace("No matches found!");
        }
    }

    myText = myText.replaceWord("cat", "dog", 0);
    trace(myText);

    I just made a new function for Strings, replaceWord, just like String.substr, String.split, String.join, etc.

    The code might look shockingly huge, but what it does, is that it analyzes each letter in the string, and then if one letter is the same as the first letter of the new word, then another code is executed, which then checks if all the letters match with the new word, if that's true as well, then the index of that word is saved inside an Array, and all other matches are saved in that Array as well. Once the analyzation is over, we check if there were found any matches, and if there were, then return the whole String, with the desired position of the found word, replaced with the new word (this is done by using String.substring, which takes out a portion of your string with the specified indexes, by this, we can take the first part of the String, before the occurred word, add the new word after that, and then add the rest of the String after the end of the found word).

    The first parameter is the word you wish to change, the second is the word you wish to replace to, and the third one is which word in the String you want to replace, for instance, in myText, there are 2 occurrances of "cat", and in this case, I replaced the first occurrance, whose index is 0, in the indexes array.

    Try changing 0 to 1, and see the difference

    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Hey Nig, I have to say, you think waaay too much!!! You are just awesome!!

    That code that you gave me earlier, that does that "replaceText" thing manually for variables is working so well.. thank you for that!

    I used that code you gave me for this situation too, so instead of:

    mytext=mytext.split("cat").join("dog")

    I did:

    mytext=mytext.substring(0, mytext.indexOf("cat"))+"dog"+mytext.substring(myte xt.indexOf("cat")+3, mytext.length);

    So that is working well. Thanks again

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Oh, you only wanted it for that text, then yeah, the shorter route is better

    No problem
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  5. #5
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Well, no yours is better! With mine I can only change the first instance (unless i use lastIndexOf), but for something in the middle, or for users choice, yeah yours is awesome! Lol Im just giving you hell for being such a brainiac! (thats a compliment of course!)

  6. #6
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Haha, to be honest, I had actually fun making this code as your question is not so commonly asked, so thanks
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center