explode function like php
1 String.prototype.explode = function(separator:String) { 2 var string = this; 3 var list = new Array(); 4 5 if (separator == null) return false; 6 if (string == null) return false; 7 8 var currentStringPosition = 0; 9 while (currentStringPosition<string.length) { 10 var nextIndex = string.indexOf(separator, currentStringPosition); 11 if (nextIndex == -1) break; 12 var word = string.slice(currentStringPosition, nextIndex); 13 list.push(word); 14 currentStringPosition = nextIndex+1; 15 } 16 if (list.length<1) { 17 list.push(string); 18 } else { 19 list.push(string.slice(currentStringPosition, string.length)); 20 } 21 return list; 22 } 23 24 var mystring = "test1 - test2"; 25 exploded = mystring.explode(" - "); 26 trace (exploded[0]); // returns 'test1'
created by leozera —
09 December 2008 —
get a short url
— tags:
actionscript
flash
prototype
—
embed