com.neolao.data.StringUtils

Méthodes utiles liées aux String.

Méthodes

Visibilité Return Nom Param 1 Param 2 Param 3 Description
static public String replace pMsg:String pOldText:String pNewText:String Remplacer un texte
static public Number str2color pMsg:String Convertir une chaine hexadécimal en nombre
static public String reverse pMsg:String Inverse l'ordre des caractères du texte
static public String lTrim pMsg:String pSpace:String Supprime au début de pMsg tous les caractères spécifiés
static public String rTrim pMsg:String pSpace:String Supprime à la fin de pMsg tous les caractères spécifiés
static public String trim pMsg:String pSpace:String Supprime au début et à la fin de pMsg tous les caractères spécifiés
static public String noAccent pMsg:String Remplace les caractères accentués par leur équivalent sans accent
static public String noPunctuation pMsg:String Supprime la ponctuation
static public String condenseWhite pMsg:String Supprime les espaces en trop dans le corps du texte
static public String isEmail pMsg:String Vérifie la validité syntaxique d'un email
static public String autoSyntax pMsg:String Reformate le texte au niveau de la syntaxe
static public String repeat pMsg:String pTotal:Number Répète un texte

Source

/**
 * ATTENTION : UTF-8 (éàöù)
 * 
 * [06/04/2004]
 * Méthodes statiques liées aux String
 * 
 * [20/10/2004] Version 2.4
 * Ajout de la méthode isEmail
 * 
 * [15/11/2004] Version 2.5
 * Ajout de la méthode noPunctuation
 * Ajout de la méthode condenseWhite
 * Ajout de la méthode autoSyntax
 * 
 * [08/11/2005] Version 2.6
 * Reécriture de _applySyntaxRule
 * 
 * [09/11/2005] Version 2.7
 * Ajout de quelques caractères de ponctuation
 * 
 * [10/11/2005] Version 2.8
 * Nouvelle méthode pour les trim
 * 
 * [13/01/2006] Version 2.9
 * Ajout de la méthode repeat
 * @ignore
 * 
 * Méthodes utiles liées aux String
 * @author 			neolao <neo@neolao.com>
 * @version			2.9 (13/01/2006)
 * @link			http://resources.neolao.com/flash/classes_fp8/data/StringUtils
 * @license			http://creativecommons.org/licenses/by-sa/2.5/
 */
class com.neolao.data.StringUtils {
	public static var className:String = "StringUtils";
	public static var version:String = "2.9";
 
	// ----------------------------- CONSTANTES --------------------------------
	/**
	 * Caractères considérés comme caractères d'espacement
	 */
	private static var DEFAULT_SPACECHARS:String =			new String(" \n\r\t");
	/**
	 * Caractères accentués et leur équivalent sans accent
	 */
	private static var ACCENT_CHARS:String =				new String("àáâãäèéêëìíîïñòóôõöùúûüçýÿÀÁÂÃÄÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÇÝŸ");	
	private static var ACCENT_CHARS_NORMAL:String =			new String("aaaaaeeeeiiiinooooouuuucyyAAAAAEEEEIIIINOOOOOUUUUCYY");
	/**
	 * Caractères de ponctuation
	 */
	private static var PUNCTUATION_CHARS:String =			new String(":¿?¡!.;,");
	/**
	 * Règles de syntaxe
	 */
	private static var SYNTAX_RULE01:String =				"xox";
	private static var SYNTAX_RULE02:String =				"xo ";
	private static var SYNTAX_RULE03:String =				" ox";
	private static var SYNTAX_RULE04:String =				" o ";
 
	// ----------------------------- VARIABLES ---------------------------------
 
	// ----------------------------- EVENEMENTS --------------------------------
 
	/*============================= CONSTRUCTEUR =============================*/
	/*========================================================================*/
 
	/*======================= FIN = CONSTRUCTEUR = FIN =======================*/
	/*========================================================================*/
 
	/*=========================== METHODES PRIVEES ===========================*/
	/*========================================================================*/
	/**
	 * Tronquer les caractères d'espacement à gauche
	 * @param pMsg Le message à tronquer
	 * @param pSpace Les caractères d'espacement
	 * @return Le message tronqué
	 */
	private static function _lTrim(pMsg:String, pSpace:String):String {
		pMsg = new String(pMsg);
		pSpace = (pSpace != undefined)?pSpace:DEFAULT_SPACECHARS;
		var i:Number = 0;
 
		while(i<pMsg.length-1 && pSpace.indexOf(pMsg.charAt(i)) != -1){
			i++;
		}
 
		return pMsg.substr(i);
	}
	/**
	 * Tronquer les caractères d'espacement à droite
	 * @param pMsg Le message à tronquer
	 * @param pSpace Les caractères d'espacement
	 * @return Le message tronqué
	 */
	private static function _rTrim(pMsg:String, pSpace:String):String {
		pMsg = new String(pMsg);
		pSpace = (pSpace != undefined)?pSpace:DEFAULT_SPACECHARS;
		var length:Number = pMsg.length - 1;
 
		while(length > 0 && pSpace.indexOf(pMsg.charAt(length)) != -1){
			length--;
		}
 
		return pMsg.substr(0,length+1);
	}
	/**
	 * Retourne l'équivalent du caractère sans accent
	 * @param pChar Le caractère
	 * @return L'équivalent sans accent
	 */
	private static function _noAccentChar(pChar:String):String{
		var i:Number = ACCENT_CHARS.indexOf(pChar);
 
		return (i != -1)?ACCENT_CHARS_NORMAL.charAt(i):pChar;
	}
	/**
	 * Supprime la ponctuation
	 * @param pChar Le caractère
	 * @return Caractère vide si c'était une ponctuation
	 */
	private static function _noPunctuationChar(pChar:String):String{
		var i:Number = PUNCTUATION_CHARS.indexOf(pChar);
 
		return (i != -1)?"":pChar;
	}
	/**
	 * Applique une règle de ponctuation sur le caractère donné
	 * @param pMsg Le message
	 * @param pPunctuation Le caractère
	 * @param pRule La règle
	 * @return Le message après le passage de la règle
	 */
	private static function _applySyntaxRule(pMsg:String, pPunctuation:String, pRule:String):String{
		// Règle avant la ponctuation
		var vRule1:String = pRule.charAt(0);
		// Règle après la ponctuation
		var vRule2:String = pRule.charAt(2);
		var vOut:String = String(pMsg);
 
		// S'il ne faut pas d'espace avant
		if(vRule1 == "x"){
			vOut = replace(vOut, " "+pPunctuation, pPunctuation);
		}
 
		// S'il ne faut pas d'espace après
		if(vRule2 == "x"){
			vOut = replace(vOut, pPunctuation+" ", pPunctuation);
		}
 
		// S'il faut un espace avant
		if(vRule1 == " "){
			vOut = replace(vOut, pPunctuation, " "+pPunctuation);
			vOut = replace(vOut, "  "+pPunctuation, " "+pPunctuation);
		}
 
		// S'il faut un espace avant
		if(vRule2 == " "){
			vOut = replace(vOut, pPunctuation, pPunctuation+" ");
			vOut = replace(vOut, pPunctuation+"  ", pPunctuation+" ");
		}
 
		return vOut;
	}
	/*===================== FIN = METHODES PRIVEES = FIN =====================*/
	/*========================================================================*/
 
	/*============================ GETTER  SETTER ============================*/
	/*========================================================================*/
 
	/*====================== FIN = GETTER  SETTER = FIN ======================*/
	/*========================================================================*/
 
	/*========================== METHODES PUBLIQUES ==========================*/
	/*========================================================================*/
	/**
	 * Remplacer un texte
	 * @param pMsg Le texte où faire le remplacement
	 * @param pOldText Le texte à remplacer
	 * @param pNewText Le nouveau texte à mettre
	 * @return Nouveau texte
	 */
	public static function replace(pMsg:String, pOldText:String, pNewText:String):String {
		return pMsg.split(pOldText).join(pNewText);
	}
	/**
	 * Convertir une chaine hexadécimal en nombre
	 * @param pMsg La chaine à convertir
	 * @return Couleur numérique
	 */
	public static function str2color(pMsg:String):Number {
		return parseInt(pMsg, 16);
	}
	/**
	 * Inverse l'ordre des caractères du texte
	 * @param pMsg Le texte à renverser
	 * @return Texte renversé
	 */
	public static function reverse(pMsg:String):String {
		var vResult:Array = pMsg.split("");
		vResult.reverse();
 
		return vResult.join("");
	}
	/**
	 * Supprime au début de pMsg tous les caractères spécifiés
	 * Si pSpace n'est pas défini, les caractères d'espacement par défaut sont utilisés.
	 * @param pMsg Le texte à traiter
	 * @param pSpace (optional) Les caractères à supprimer
	 * @return Chaine sans les caractères spéficiés au début
	 */
	public static function lTrim(pMsg:String, pSpace:String):String {
		return _lTrim(pMsg, pSpace);
	}
	/**
	 * Supprime à la fin de pMsg tous les caractères spécifiés
	 * Si pSpace n'est pas défini, les caractères d'espacement par défaut sont utilisés.
	 * @param pMsg Le texte à traiter
	 * @param pSpace (optional) Les caractères à supprimer
	 * @return Chaine sans les caractères spéficiés à la fin
	 */
	public static function rTrim(pMsg:String, pSpace:String):String {
		return _rTrim(pMsg, pSpace);
	}
	/**
	 * Supprime au début et à la fin de pMsg tous les caractères spécifiés
	 * Si pSpace n'est pas défini, les caractères d'espacement par défaut sont utilisés.
	 * @param pMsg Le texte à traiter
	 * @param pSpace Les caractères à supprimer
	 * @return Chaine sans les caractères spéficiés au début et à la fin
	 */
	public static function trim(pMsg:String, pSpace:String):String {
		return _rTrim(_lTrim(pMsg, pSpace), pSpace);
	}
	/**
	 * Remplace les caractères accentués par leur équivalent sans accent
	 * @param pMsg Le texte à traiter
	 * @return Texte sans caractères accentués
	 */
	public static function noAccent(pMsg:String):String{
		pMsg = new String(pMsg);
		var r:String = "";
		var l:Number = pMsg.length;
 
		for(var i:Number=0; i<l; i++){
			r += _noAccentChar(pMsg.charAt(i));
		}
 
		return r;
	}
	/**
	 * Supprime la ponctuation
	 * @param pMsg Le texte à traiter
	 * @return Texte sans ponctuation
	 */
	public static function noPunctuation(pMsg:String):String{
		pMsg = new String(pMsg);
		var r:String = "";		
		var l:Number = pMsg.length;
 
		for(var i:Number=0; i<l; i++){
			r += _noPunctuationChar(pMsg.charAt(i));
		}
		return r;
	}
	/**
	 * Supprime les espaces en trop dans le corps du texte
	 * @param pMsg Le texte à traiter
	 * @return Texte sans espaces
	 */
	public static function condenseWhite(pMsg:String):String{
		var a:Array = pMsg.split(" ");
		var r:String = "";
		var i:Number = 0;
 
		while(a[i] != undefined){
			if(a[i] == ""){
				a.splice(i, 1);
			}else{
				i++;
			}
		}
 
		return a.join(" ");
	}
	/**
	 * Vérifie la validité syntaxique d'un email
	 * Il manque encore des vérifications, mais ca suffit pour l'instant
	 * @param pMsg La chaine à vérifier
	 * @return true si la chaine est un email, sinon false
	 */
	public static function isEmail(pMsg:String):Boolean {
		pMsg = new String(pMsg);
		var length:Number = pMsg.length;
		var cars:String = new String("abcdefghijklmnopqrstuvwxyz0123456789-.@_");
		var mail:Array = pMsg.split("@");
		var user:Array = mail[0];
		var domain:Array = mail[1].split(".");
		var i:Number;
 
		// La taille minimum est de 6 "a@b.cc";
		if (length < 6) {
			return false;
		}
		// user commence par une lettre
		if(cars.indexOf(user.charAt(0)) >= 26){
			return false;
		}
		// Vérifie les caractères valides
		for(i=0; i<length; i++){
			if(cars.indexOf(pMsg.charAt(i)) == -1){
				return false;
			}
		}
		// Il n'y a qu'un et unique @
		if (mail.length != 2) {
			return false;
		}
		// Il y a au moins 2 parties après le @ "@b.cc.dd.ee.ff"
		if (domain.length < 2) {
			return false;
		}
		// Chaque partie du domaine ne doit pas être vide et ne contient pas de _
		for(i=0; i<domain.length; i++){
			if(domain[i] == "" || domain[i].indexOf("_") != -1){
				return false;
			}
		}
		// Il y a au moins 2 caractères après le dernier .
		if (domain[domain.length - 1].length < 2) {
			return false;
		}
		return true;
	}
	/**
	 * Reformate le texte au niveau de la syntaxe
	 * @param pMsg Le texte à traiter
	 * @return Texte reformaté
	 */
	public static function autoSyntax(pMsg:String, pLanguage:String):String{
		var vLang:String = (pLanguage == undefined)?"fr":pLanguage;
		var vOut:String;
		var i:Number;
 
		vOut = condenseWhite(pMsg);
 
		switch(pLanguage){
			// Espagnol
			case "es":
				vOut = _applySyntaxRule(vOut, ",", SYNTAX_RULE02);
				vOut = _applySyntaxRule(vOut, ".", SYNTAX_RULE02);
				vOut = _applySyntaxRule(vOut, ";", SYNTAX_RULE02);
				vOut = _applySyntaxRule(vOut, ":", SYNTAX_RULE02);
				vOut = _applySyntaxRule(vOut, "¿", SYNTAX_RULE03);
				vOut = _applySyntaxRule(vOut, "?", SYNTAX_RULE02);
				vOut = _applySyntaxRule(vOut, "¡", SYNTAX_RULE03);
				vOut = _applySyntaxRule(vOut, "!", SYNTAX_RULE02);
				break;
			default:
			case "fr":
				vOut = _applySyntaxRule(vOut, ",", SYNTAX_RULE02);
				vOut = _applySyntaxRule(vOut, ".", SYNTAX_RULE02);
				vOut = _applySyntaxRule(vOut, ";", SYNTAX_RULE04);
				vOut = _applySyntaxRule(vOut, ":", SYNTAX_RULE04);
		}
 
		return trim(vOut);
	}
	/**
	 * Répète un texte
	 * @param pMsg Le texte à répéter
	 * @param pTotal Le nombre de fois que le texte est répété
	 * @return Le texte répété pTotal fois
	 */
	public static function repeat(pMsg:String, pTotal:Number):String{
		var o:String = "";
		for(var i:Number=1; i<=pTotal; i++){
			o += pMsg;
		}
		return o;
	}
	/*==================== FIN = METHODES PUBLIQUES = FIN ====================*/
	/*========================================================================*/
}