View Full Version : Eliminating letter of a string
El Bicho
January 27th, 2004, 05:43 PM
This code just does what it says.
myString="This code eliminates letter \"o\"."
letterKill =function(letter){
while(found!=-1){
found=myString.indexOf(letter)
if(found!=-1){
myString=myString.slice(0,found)+myString.slice(fo und+1)
}
}
}
letterKill("o")
trace(myString)
I have two questions:
1)How can I make it eliminate more than one letter.
2)If I want to eliminate all the "t`s" , how do I eliminate the "T" (capital).
SoloDesignz
January 27th, 2004, 05:59 PM
myString="This code eliminates letter \"o\"."
letterKill =function(letter){
found=myString.indexOf(letter)
while(found!=-1){
found=myString.indexOf(letter)
if(found!=-1){
myString=myString.slice(0,found)+myString.slice(fo und+1)
}
}
}
letterKill("o");
trace(myString)
letterKill("t");
trace(myString);
letterKill("T");
trace(myString);
stop();
works somewhat =)
El Bicho
January 27th, 2004, 10:27 PM
Thanks for the reply. I finally found a way:
myString = "This code eliminates letter \"o\".";
letters = ["o", "t"];
letterKill = function (array) {
for (i = 0; i < letters.length; i++) {
found = myString.indexOf(array[i]);
while (found != -1) {
found = myString.toLowerCase().indexOf(array[i]);
if (found != -1) {
myString = myString.slice(0, found) + myString.slice(found + 1);
}
}
}
};
letterKill(letters);
trace(myString);
norie
January 27th, 2004, 11:25 PM
String.prototype.removeChar = function(removeChar, replaceWith) {
var sepString = this.split(removeChar);
return String(sepString.join(replaceWith));
};
code = code.removeChar("\"", "\\\"");
code = code.removeChar("\t", " ");
code = code.removeChar("\'", "\\\'");
code = code.removeChar("<", "<");
code = code.removeChar(">", ">");
code = code.removeChar("\r", "<BR>");
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.