2 Replies Latest reply on Oct 25, 2006 1:02 PM by csthsl

    trim string

    pubchum
      what is the built in trim method to shorten a string from:
      "fjalfjlsfja;dlsfjkfa;sldkfjalsdfjadjf24"
      to just "24"

      So I can use the number from the Instance Name to find the corresponding file name in an Array?
        • 1. Re: trim string
          macdaddy256
          If you can always guarantee that the number will be two digits at the end of the string, use:

          theSubstring = theString.substr(theString.length-2);
          trace(theSubString);

          Don't forget to switch the string to a number if that's what you need for the rest of the code.
          • 2. Re: trim string
            csthsl
            There is no built in trim function. But that doesn't mean we can't make em!
            Here's what I did. Works great!

            /*------------------------
            Creator: Steve Holland
            Fusecast.com
            Date: 10/25/06
            ------------------------*/
            //Left Trim
            function ltrim(string,count){
            var newString = string.split('');
            var charFound = false;
            var returnString = "";
            if(!count){
            var loopcount = newString.length;
            }else{
            var loopcount = parseInt(count);
            }
            if(newString.length > 0){
            //loop over the string forward
            for(i=0;i<loopcount;i++){
            if(newString != " "){
            charFound = true;
            }
            if(charFound){
            returnString = returnString + newString
            ;
            }
            }
            return returnString;
            }else{
            return returnString;
            }
            }

            //Right Trim
            function rtrim(string,count){
            var newString = string.split('');
            var charFound = false;
            var returnString = "";
            if(!count){
            var loopcount = newString.length - 1;
            }else{
            var loopcount = (newString.length - 1) - parseInt(count);
            }
            if(newString.length > 0){
            //loop over the string forward
            for(i=loopcount;i>-1;i--){
            if(newString != " "){
            charFound = true;
            }
            if(charFound){
            returnString = newString
            + returnString;
            }
            }
            return returnString;
            }else{
            return returnString;
            }
            }

            //Trim function
            function trim(string){
            return ltrim(rtrim(string));
            }

            --------------------------------
            To use it you just do this:
            var mytrim = rtrim("1234567890",4);
            trace(mytrim); // returns "123456"

            If you leave the second argument off of the function call (E.g. rtrim("1234567890"); the function will trim off any space charaters at the end of the string.

            the ltrim() function removes empty space characters at the beginning of the string if no second argument is pass into the function.

            the trim() function removes empty characters at the beginning and end of the string! Use this for cleaning up your data.

            Good luck!