A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Need help with a string to array function

  1. #1
    Member
    Join Date
    Oct 2008
    Posts
    37

    Need help with a string to array function

    Im looking for a string function similar to split, i need to find text within a string and store it in an array...

    For example..

    Useless text i dont need
    [start]this is the text i need to find and store[finish]
    useless text i dont need
    [start]store and find this too[finish]
    useless text...

    so locate all text within the [start] and [finish] and add it into an array.
    so the above string would produce

    array[0]=this is the text i need to find and store;
    array[1]=store and find this too;

    Is there such a function?

    Thanks.

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Nope, no such functions exists, but why not create your own ?

    Since you can't directly use ENTERs (or linebreaks) in Flash, I've replaced them with \n, which is the same only with code.

    Actionscript Code:
    myString = "Useless text i dont need\n[start]this is the text i need to find and store[finish]\nuseless text i dont need\n[start]store and find this too[finish]";
    myArray = new Array();

    stringToArray(myString, myArray, "[start]", "[finish]");

    trace(myArray);

    ///// THE FUNCTION /////

    function stringToArray(string, array, startText, endText){
        var theString = string;
        var i = 0;
       
        extractInfo();
       
        function extractInfo(){
            if(theString.indexOf(startText) != -1){
                array[i] = theString.substring(theString.indexOf(startText)+startText.length, theString.indexOf(endText));
                theString = theString.substring(theString.indexOf(endText)+endText.length, theString.length);
                i++;
                extractInfo();
            } else {
                return false;
            }
        }
    }

    ////////////////////
    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