Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   Java (http://www.forosdelweb.com/f45/)
-   -   Como funciona el Trim() (http://www.forosdelweb.com/f45/como-funciona-trim-371596/)

zyon 16/02/2006 09:24

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!!!:-)

stock 16/02/2006 09:46

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 :-D

have funnnnnnnnnnnnnnn :adios:

PyroZ 16/02/2006 11:17

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 :-D

have funnnnnnnnnnnnnnn :adios:

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

stock 16/02/2006 11:43

:neurotico

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 :cool:

Fonchero 31/07/2008 15:47

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);
}


La zona horaria es GMT -6. Ahora son las 19:04.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.