Handle spaces in Strings with Regular Expresions - ActionScript 3 | Phoxer.com
Handle spaces in Strings with Regular Expresions - ActionScript 3
03/10/2008


The handling of strings, forms can be a real headache when it comes to validating the data.
Luckily there in ActionScript3 Regular Expressions "regexp"

So I Developed a series of classes to validate and manage Strings and Regular Expressions and one of them is my class TrimMode.
TrimMode class manages the different types of spaces between characters in a string

let's see how it works:

import phoxer.Strings.TrimMode;
 
TrimMode.trim("  A *ver!	?$&   que 467 	oNda     "); //A *ver!	?$&   que 467 	oNda
TrimMode.removeSpaces("  A *ver!	?$&   que 467 	oNda     "); //A*ver!?$&que467oNda
TrimMode.removeExtraSpaces(" 	w  A*ver	w!?$   &q		ue 467 oNda  	w    "); //w A*ver w!?$ &q ue 467 oNda w 
TrimMode.addSpaces(" 	w  A*ver	w!?$   &q		ue 467 oNda  	w    "); // w A * v e r w ! ? $ & q u e 4 6 7 o N d a w
 

As we saw, i Developed 4 different methods to handle the spaces of the Strings:
-TrimMode.trim: Removes spaces at the beginning and end of the Strings.
-TrimMode.removeSpaces: Removes all spaces from the string.
-TrimMode.removeExtraSpaces: Removes all double spaces.
-TrimMode.addSpaces: Removes all double spaces, and separates all the characters of the string.

Here is my class TrimMode:

/**
by .:[PHOXER]:.
http://www.phoxer.com
v 1.0;
*/
package phoxer.Strings{
	public class TrimMode{
		public static function trim(str:String):String{
			return str.replace(/^\s*(.*?)\s*$/g, "$1");
		}
 
		public static function removeAllSpaces(str:String):String{
			return str.replace(/\s*(.*?)/g, "$1");
		}
 
		public static function removeExtraSpaces(str:String):String{
			return trim(str).replace(/\s{1,}(.*?)/g, "$1 ");
		}
 
		public static function splitInSpaces(str:String):String{
			return trim(removeAllSpaces(str).replace(/\s*(.*?)/g, " "));
		}
	}
}




Canal Rss