This is not a blog. So sue me!


Crikey, things are looking up!

Thursday, November 15, 2007

removeString: ActionScript 2.0 function to remove delimited strings

This function is quite useful.
It will remove strings that are delimited by known characters, any number of times from a target string. For example to remove all the markups in an XML node and just leave the text, call it with this snippet:
tempString = pNode.toString();
while (tempString.indexOf("<") != -1) {
tempString = removeString(tempString,"<",">");
}

function removeString(input:String, sDelim:String, 
eDelim:String):String {
var startIndex:Number = 0;
var sDelimIndex:Number = input.indexOf(sDelim);
var eDelimIndex:Number = input.indexOf(eDelim);
var endIndex:Number = input.length-1;

var output:String = "";

if (input == undefined ||
input == "" ||
sDelimIndex != -1) {
//get the substring before the delimiter, if any
if (startIndex < sDelimIndex) {
output += input.substring(startIndex,sDelimIndex);
}
if (eDelimIndex != -1) { // if any end delim
// get the substring after the delimiter, if any
if (eDelimIndex < endIndex) {
output += input.substring(eDelimIndex+1,endIndex+1);
}
}
}
else {
//nothing to be done
output = input;
}
return output;
}

No comments:

Contributors

St Lawrence Rowing

Test content from SLRC