// replace findThis string from stringIn with replaceWith, repeat number of times
//repeat must be >=1, anything else it will do 1 repeat anyway
// if the string doesn't occur nothing will happen
public static function replaceString(stringIn:String, findThis:String, replaceWith:String, repeat:Number):String {
var stringOut:String = '';
var tempArr:Array;
tempArr = stringIn.split(findThis); //spilt the string into an array based on the occurrences of the target string
stringOut = tempArr[0]; //get the first split item
for (var i=1;i < tempArr.length;i++){
if (i < repeat+1){
stringOut = stringOut + replaceWith + tempArr[i];// replace the next occurrence and add the next split item
}
else {
stringOut = stringOut + findThis + tempArr[i];// restore the rest of the string as it was
}
}
trace("String in: " + stringIn + " String out: " + stringOut);
return stringOut;
}
This is not a blog.
So sue me!
I don't want to keep explaining. It's formatted like this so that code will fit without wrapping. I don't care if you think it's ugly.
Blog Archive
Wednesday, October 15, 2008
ActionScript 2; replace string any number of times
The cheeseparing number of useful String class methods released by Adobe leaves something for the programmer to do. Here is a replace function that will replace a substring within another string any number of times. It starts at the first occurrence.
Subscribe to:
Post Comments (Atom)
Labels
- Funny (14)
- rant (9)
- reviews (7)
- spam sucks (7)
- stolen joke alert (7)
- boaters (6)
- self-indulgence (6)
- links (5)
- actionscript 2 (4)
- programming (4)
- sailing (4)
- sex (4)
- Windows command line (3)
- Windows desktop (3)
- code (3)
- computers (3)
- flash (3)
- health (3)
- movies (3)
- music (3)
- photos (3)
- Flash CS3 (2)
- books (2)
- browsers (2)
- cooking (2)
- politics (2)
- songs (2)
- user interface (2)
- Eclipse (1)
- IDEs (1)
- Subversion (1)
- age (1)
- amazing (1)
- batch files (1)
- blogging (1)
- cats (1)
- color (1)
- government (1)
- html (1)
- java (1)
- jsp (1)
- language (1)
- management (1)
- olpc (1)
- philosophy (1)
- photography (1)
- quotes (1)
- safety (1)
- sales (1)
- santa (1)
- sociology (1)
- special characters (1)
- tools (1)
- webapps (1)
0 comments:
Post a Comment