J'essaye de découper des espaces blancs de l'avant et de l'extrémité d'une chaîne. On dirait que as3 n'a pas de fonction de trim. Quelqu'un sait-il comment cela peut-il être fait?Rognage des chaînes avec actionscript 3
Répondre
Regardez http://jeffchannell.com/ActionScript-3/as3-trim.html
function trim(s:String):String
{
return s.replace(/^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2");
}
http://www.designscripting.com/2008/11/string-utils-in-as3/ a une tonne de fonctions utilitaires de chaîne, y compris la chaîne garniture
Vous devriez être en mesure d'utiliser regex, quelque chose comme:
var pattern:RegExp = /(\t|\n|\s{2,})/g;
trimmedString = untrimmedString.replace(pattern, '');
Avez-vous consulter la documentation d'Adobe? http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/String.html
package {
import flash.display.Sprite;
public class StringExample extends Sprite {
public function StringExample() {
var companyStr:String = new String(" Company X");
var productStr:String = "Product Z Basic ";
var emptyStr:String = " ";
var strHelper:StringHelper = new StringHelper();
var companyProductStr:String = companyStr + emptyStr + productStr;
trace("'" + companyProductStr + "'"); // ' Company X Product Z Basic '
companyProductStr = strHelper.replace(companyProductStr, "Basic", "Professional");
trace("'" + companyProductStr + "'"); // ' Company X Product Z Professional '
companyProductStr = strHelper.trim(companyProductStr, emptyStr);
trace("'" + companyProductStr + "'"); // 'Company X Product Z Professional'
}
}
}
classe StringHelper {
public function StringHelper() {
}
public function replace(str:String, oldSubStr:String, newSubStr:String):String {
return str.split(oldSubStr).join(newSubStr);
}
public function trim(str:String, char:String):String {
return trimBack(trimFront(str, char), char);
}
public function trimFront(str:String, char:String):String {
char = stringToCharacter(char);
if (str.charAt(0) == char) {
str = trimFront(str.substring(1), char);
}
return str;
}
public function trimBack(str:String, char:String):String {
char = stringToCharacter(char);
if (str.charAt(str.length - 1) == char) {
str = trimBack(str.substring(0, str.length - 1), char);
}
return str;
}
public function stringToCharacter(str:String):String {
if (str.length == 1) {
return str;
}
return str.slice(0, 1);
}
}
MISE À JOUR: Oh juste vu ItzWarty. -.-
str = str.replace(/^\s+|\s+$/g, '');
Vous avez une méthode qui est appelée garniture dans la classe utilitaire appelé StringUtil =>http://livedocs.adobe.com/flex/3/langref/mx/utils/StringUtil.html#trim%28%29
solution de ItzWarty ne supprime pas les espaces de fin.
La solution d'Amargosh fonctionne parfaitement pour moi, et est également donnée au http://jeffchannell.com/ActionScript-3/as3-trim.html.
Malheureusement, je n'ai pas assez de réputation pour voter pour la solution d'Amargosh.
De plus j'ai dû enlever doublequotes donc voici mon assiette:
function trim(s:String):String
{
return s.replace(/^[\s|"]+|[\s|"]+$/gs, '');
}
Essayez cette
str = StringUtil.trim(str);
- 1. Littéral de chaînes multiples dans Actionscript 3
- 2. arsort avec Actionscript 3
- 3. Actionscript 3 3d avec fieldofview
- 4. Liste des événements Actionscript 3
- 5. Comment comparer par ordre alphabétique deux chaînes dans Actionscript 3
- 6. actionscript 2 à actionscript 3
- 7. Flash Whiteboard: Actionscript 3
- 8. ActionScript 2 - ActionScript 3: sous-ensemble commun?
- 9. Le rognage de chaînes provoque une fuite de mémoire?
- 10. Actionscript 3 Sprite AddChild
- 11. ActionScript 3 et SSH
- 12. Décharger un Loader avec actionscript 3
- 13. Actionscript 3 requête MySQL avec PHP
- 14. Comment passer des arguments avec 'nouveau répondeur'? actionscript 3
- 15. Organiser (z) l'ordre des objets dans Flash avec ActionScript 3?
- 16. Déclarant une chaîne avec des citations dans ActionScript 3
- 17. ActionScript 3 problème
- 18. Actionscript 3 Scrollbar
- 19. Jeu de Rollercoaster avec ActionScript 3
- 20. Menu Actionscript 3 avec état actif
- 21. Actionscript 3 Bogue TextField avec htmlText?
- 22. Sounds fade in/out avec ActionScript 3
- 23. ActionScript 3 Étiquettes Gras
- 24. actionscript 3 init()
- 25. Composants pour actionscript 3
- 26. Écoute efficace des événements dans ActionScript 3
- 27. Passer des variables ByRef dans Actionscript 3
- 28. Floodfill dans ActionScript 3
- 29. actionscript 3 et JSON
- 30. ActionScript 3 Distributing TextFields
Chose curieuse, je suis arrivé à cette page en googler;) –
Je ne suis pas fan de cette toute l'attitude «juste google it» qui se passe de plus en plus sur SO. Trawling à travers des blogs aléatoires pour des bribes d'informations n'est pas la même que d'obtenir l'aide collective d'une ressource de programmation comme SO - SO est conçu pour que les bonnes réponses flottent au sommet sur la base des opinions de nombreux, pas un seul blogueur. – Chris
@Chris et Hendy, j'ai également obtenu ici de Google. – BajaBob