Estoy tratando de recortar espacios en blanco desde el frente y el final de una cadena. Parece que as3 no tiene una función de recorte. ¿Alguien sabe cómo se puede hacer?recorte de cadenas con actionscript 3
Respuesta
Mira 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/ tiene un montón de funciones de utilidad cadena, incluyendo cadena de recortar
Usted debe ser capaz de utilizar expresiones regulares, algo así como:
var pattern:RegExp = /(\t|\n|\s{2,})/g;
trimmedString = untrimmedString.replace(pattern, '');
¿Te ha verifique la documentación de 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'
}
}
}
StringHelper clase {
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);
}
}
ACTUALIZACIÓN: Oh, sólo vio ItzWarty. -.-
str = str.replace(/^\s+|\s+$/g, '');
Tiene un método que se llama ajuste de la clase de utilidad llama StringUtil =>http://livedocs.adobe.com/flex/3/langref/mx/utils/StringUtil.html#trim%28%29
solución de ItzWarty no quita espacios finales.
La solución de Amargosh funciona perfectamente para mí, y también se ofrece en http://jeffchannell.com/ActionScript-3/as3-trim.html.
Desafortunadamente no tengo suficiente reputación para votar por la solución de Amargosh.
Además tuve que quitar comillas dobles así que aquí está mi ajuste:
function trim(s:String):String
{
return s.replace(/^[\s|"]+|[\s|"]+$/gs, '');
}
probar este
str = StringUtil.trim(str);
- 1. Descarga de un ByteArray con Actionscript 3
- 2. Depuración Haxe (ActionScript 3)
- 3. actionscript 3 y JSON
- 4. Colecciones Inmutables Actionscript 3
- 5. Trigonometría triangular (ActionScript 3)
- 6. actionscript 3 init()
- 7. Actionscript 3 REPL
- 8. Actionscript 3 - Importar archivo SVG
- 9. ActionScript 3: animación programática Smooth
- 10. Dynamic Audio Generation Actionscript 3
- 11. Herencia múltiple en ActionScript 3
- 12. ActionScript 3 ¿Especificación como PDF?
- 13. Obtener tamaño de diccionario de ActionScript 3
- 14. Descripción de Array :: empalme en ActionScript 3
- 15. Actionscript 3 fuera de Flash/Flex
- 16. Ampliación de matriz en Actionscript 3 (Flex)
- 17. Obtener URL del navegador actual - ActionScript 3
- 18. as3 matriz aleatoria - aleatorizar matriz - actionscript 3
- 19. ¿Cómo funciona Vector map() en actionscript 3?
- 20. Insertar mp3 en ActionScript 3 no funciona
- 21. ¿Algún recurso para juegos Pure Actionscript 3?
- 22. Comunicación entre C++ y actionscript 3
- 23. traer objeto al flash frontal actionscript 3
- 24. Actionscript 3: ¿Cargando archivos ai dinámicamente?
- 25. Validación de esquema no recorte de cadenas antes de validar
- 26. Actionscript 3 ExternalInterface, Pass variable to javascript?
- 27. Dibujar texto en forma en ActionScript 3
- 28. ¿ActionScript-3 no puede tener múltiples constructores?
- 29. ¿Es el diccionario ActionScript 3 un hashmap?
- 30. Recorte simple con Paperclip
Curiosamente, llegué a esta página, mientras que buscando en Google;) –
No soy un fan de este toda la actitud de "solo google it" que está sucediendo cada vez más en SO. Navegar por blogs aleatorios para obtener fragmentos de información no es lo mismo que obtener la ayuda colectiva de un recurso de programación como SO - SO, diseñado para que las respuestas excelentes lleguen a la cima según las opiniones de muchos, no solo de un bloguero. – Chris
@Chris y Hendy, también llegué aquí de Google. – BajaBob