Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Bryan Gidge http://www.gidge.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Actionscript _String Class

   1  dynamic class _String {
   2  	// Replace a string or substrings within a string
   3  	static function Replace (the_String, search_String, replace_String, occurrences, backward) {
   4  		if (search_String == replace_String) return the_String;
   5  		var found = 0;
   6  		if (backward == true) {
   7  			var pos = the_String.lastIndexOf(search_String);
   8  			while (pos>= 0) {
   9  				found++;
  10  				var start_String = the_String.substr(0, pos);
  11  				var end_String = the_String.substr(pos + search_String.length);
  12  				the_String = start_String + replace_String + end_String;
  13  				pos = the_String.lastIndexOf(search_String, start_String.length);
  14  				if (found == occurrences) pos = -1;
  15  			}
  16  		}
  17  		else {
  18  			var pos = the_String.indexOf(search_String);
  19  			while (pos>= 0) {
  20  				found++;
  21  				var start_String = the_String.substr(0, pos);
  22  				var end_String = the_String.substr(pos + search_String.length);
  23  				the_String = start_String + replace_String + end_String;
  24  				pos = the_String.indexOf(search_String, pos + replace_String.length);
  25  				if (found == occurrences) pos = -1;
  26  			}
  27  		}
  28  		
  29  		return the_String;
  30  	}
  31  	
  32  	// Convert delimited (comma by default) String to an Array
  33  	static function toArray(string, separator:String) {
  34  		var list = new Array();
  35  		if (typeof(string) == "string"){
  36  			if (separator == undefined) separator = ",";
  37  			if (string == null) return false;
  38  			var currentStringPosition = 0;
  39  			while (currentStringPosition<string.length) {
  40  				var nextIndex = string.indexOf(separator, currentStringPosition);
  41  				if (nextIndex == -1) break;
  42  				var word = string.slice(currentStringPosition, nextIndex);
  43  				list.push(word);
  44  				currentStringPosition = nextIndex+1;
  45  			}
  46  			if (list.length<1) list.push(string);
  47  			else list.push(string.slice(currentStringPosition, string.length));
  48  		} else {
  49  			list.push(string);
  50  		}
  51  		return list;
  52  	}
  53  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS