-
Senior Member
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);
-
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
-
[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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|