dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Removing undesired chars from a string

  1. #1
    Senior Member maksum's Avatar
    Join Date
    Oct 2002
    Location
    Portland, OR
    Posts
    118

    Removing undesired chars from a string

    I want to take a string and strip out specified characters. In the case of this sample, I'm removing "(", ")", and ";".

    I'm taking the string, and placing each character in an array if is ISN'T one of my no-no characters, but then I can't get it back to a string. I'm using toString() but unfortunately it includes the commas, which I don't want.

    Can someone help me figure out how to turn this back into a regular string?

    OR is there a simpler way to accomplish my objective?

    Thanks so much in advance,

    Mike

    code:

    var testName = "()hello();;";
    trace("testName = " + testName);
    testNameArray = new Array();
    for(i=0;i<testName.length;i++){
    newChar = testName.charAt(i);
    trace("char " + i + " = " + newChar);
    if((newChar != "(") && (newChar != ")") && (newChar != ";")){
    testNameArray.push(newChar);
    }
    }
    trace("testNameArray = " + testNameArray);
    testName = testNameArray.toString();
    trace("testName = " + testName);


  2. #2
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    You can use Array.join(''), (that's two single quotes) which joins the array into a string using the given character (in this case, an empty string) as the separator.

    An easier solution is to test the characters and then, if they are keepers, simply append them to the end of your new string:

    code:

    newString = '';
    for(var i = 0; i < oldString.length; i++) {
    c = oldString.charAt(i);
    if(blah blah blah) {
    newString += c;
    }
    }

    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  3. #3
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    [
    code:
    s = "here are some words, we'll take the letter e out of";
    t = s.split("e").join("");
    trace(t);


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