Retroceder   Foros del Web > Programación para sitios web > Java y JSP

Respuesta
 
Herramientas Desplegado
Antiguo 16-feb-2006, 08:24   #1 (permalink)
zyon ha deshabilitado el karma
 
Fecha de Ingreso: septiembre-2005
Mensajes: 960
Pregunta Como funciona el Trim()

Que tal, un saludo a todos, alguien podria decirme como utilizar el trim(), es que veo el api de JAVA pero la neta no lo comprendo...De antemano gracias!!!
zyon está desconectado   Responder Citando
Antiguo 16-feb-2006, 08:46   #2 (permalink)
stock tiene algunos puntos positivos de karma
 
Avatar de stock
 
Fecha de Ingreso: junio-2004
Ubicación: Monterrey NL
Mensajes: 1.995
String texto = "este es el texto ok?";

//Esta es la salida normal "este es el texto ok?"
System.out.println(texto);

//esta es la salida haciendole un trim "esteeseltextook?"
System.out.println(texto.trim());

si te fijas unicamnete le quita los espacios en blanco

have funnnnnnnnnnnnnnn
__________________
Don't hate the languaje, hate the runtime environment
Crysfel's Blog :: Blog de programación, JAVA,PHP, AJAX, JavaScript, CSS y otras hierbas
stock está desconectado   Responder Citando
Antiguo 16-feb-2006, 10:17   #3 (permalink)
PyroZ no se puede cailificar en este momento
 
Fecha de Ingreso: diciembre-2005
Mensajes: 44
Cita:
Iniciado por stock
String texto = "este es el texto ok?";

//Esta es la salida normal "este es el texto ok?"
System.out.println(texto);

//esta es la salida haciendole un trim "esteeseltextook?"
System.out.println(texto.trim());

si te fijas unicamnete le quita los espacios en blanco

have funnnnnnnnnnnnnnn
Estas equivocado Stock, no quita los espacios en blanco de entre los caracteres, sino lo de los extremos, funciona igual que el TRIM de Visual Basic

String texto = " este es el texto ok? ";

// Esta es la salida normal "este es el texto ok?"
System.out.println(texto);

// esta es la salida haciendole un trim "este es el texto ok?"
System.out.println(texto.trim());

Recordar que cuidado con los objetos String que son Objetod inmutables, es decir no se pueden modificar.

saludos
PyroZ está desconectado   Responder Citando
Antiguo 16-feb-2006, 10:43   #4 (permalink)
stock tiene algunos puntos positivos de karma
 
Avatar de stock
 
Fecha de Ingreso: junio-2004
Ubicación: Monterrey NL
Mensajes: 1.995


thatś rigth

Cita:
trim

public String trim()

Removes white space from both ends of this string.

If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.

Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).

This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well.

Returns:
this string, with white space removed from the front and end.
siempre se aprende algo nuevo
__________________
Don't hate the languaje, hate the runtime environment
Crysfel's Blog :: Blog de programación, JAVA,PHP, AJAX, JavaScript, CSS y otras hierbas
stock está desconectado   Responder Citando
Antiguo 31-jul-2008, 15:47   #5 (permalink)
Fonchero ha deshabilitado el karma
 
Fecha de Ingreso: abril-2007
Mensajes: 3
Respuesta: Como funciona el Trim()

Asi funciona el trim

public String trim () {
int start = offset, last = offset + count - 1;
int end = last;
while ((start <= end) && (value [start] <= ' ')) start++;
while ((end >= start) && (value [end] <= ' ')) end--;
if (start == offset && end == last) return this;
return new String (start, end - start + 1, value);
}
Fonchero está desconectado   Responder Citando
Respuesta

No hay votos aún.


Herramientas
Desplegado

Normas de Publicación
No puedes crear nuevos temas
No puedes responder temas
No puedes subir archivos adjuntos
No puedes editar tus mensajes

BB code is Activado
Caritas están Activado
[IMG] está Desactivado
Código HTML está Desactivado


La Zona horaria es GMT -6. Ahora son las 11:51.


Message Board Statistics

LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93