Foros del Web » Programación para mayores de 30 ;) » Java »

Como funciona el Trim()

Estas en el tema de Como funciona el Trim() en el foro de Java en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 16/02/2006, 09:24
 
Fecha de Ingreso: septiembre-2005
Mensajes: 1.289
Antigüedad: 18 años, 7 meses
Puntos: 3
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!!!
  #2 (permalink)  
Antiguo 16/02/2006, 09:46
Avatar de stock  
Fecha de Ingreso: junio-2004
Ubicación: Monterrey NL
Mensajes: 2.390
Antigüedad: 19 años, 10 meses
Puntos: 53
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
  #3 (permalink)  
Antiguo 16/02/2006, 11:17
 
Fecha de Ingreso: diciembre-2005
Mensajes: 44
Antigüedad: 18 años, 4 meses
Puntos: 0
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
  #4 (permalink)  
Antiguo 16/02/2006, 11:43
Avatar de stock  
Fecha de Ingreso: junio-2004
Ubicación: Monterrey NL
Mensajes: 2.390
Antigüedad: 19 años, 10 meses
Puntos: 53


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
  #5 (permalink)  
Antiguo 31/07/2008, 15:47
 
Fecha de Ingreso: abril-2007
Mensajes: 3
Antigüedad: 17 años
Puntos: 0
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);
}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:28.