Está en la página 1de 3

Class StringTokenizer

java.lang.Object

o java.util.StringTokenizer

All Implemented Interfaces:


Enumeration<Object>

public class StringTokenizer


extends Object
implements Enumeration<Object>

The following is one example of the use of the tokenizer. The code:
StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}

prints the following output:


this
is
a
test

is a legacy class that is retained for compatibility reasons although


its use is discouraged in new code. It is recommended that anyone seeking this
functionality use the split method of String or the java.util.regex package instead.
StringTokenizer

The following example illustrates how the String.split method can be used to break
up a string into its basic tokens:
String[] result = "this is a test".split("\\s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);

prints the following output:


this
is
a
test

Para contar caracteres por ejemplo Variable x

Seria

x.length()

import java.util.StringTokenizer;
class PruebaST{
static String entrada =
"Ronaldo=Futbol;Gasol=Baloncesto;Nadal=Tenis;Jordan=Baloncesto;";
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer(entrada, "=;");
while(st.hasMoreTokens()) {
String jugador = st.nextToken();
String deporte = st.nextToken();
System.out.println(jugador + " " + deporte);
}
}
}

Resultado
La salida que dara ese cdigo sera la siguiente:
Ronaldo Futbol
Gasol Baloncesto
Nadal Tenis
Jordan Baloncesto

1. String sCadena = "Esto es una cadena de texto";

Ahora tenemos que saber que los caracteres empiezan a enumerarse por el 0. Es por ello
que si queremos obtener el primer carcter tenemos que solicitar el 0, y si queremos el
carcter 'e' de la palabra 'es', este ser el quinto carcter.
De esta manera el cdigo Java con el mtodo .charAt() se queda de la siguiente forma:
1. System.out.println(sCadena.charAt(5));

Resultado
e

También podría gustarte