ola pessoal, eu quero saber como eu faço pra pegar a segunda ou terceira palavra de uma string para comprar com um vertor de strings
se alguem poder me ajudar fico grato…
Utilize StringTokenizer, passando espaço em branco para delimitação dos tokens.
Ai você pode pegar o segundo ou o terceiro ou qual você quiser.
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
http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
Um exemplo sucinto!
Faça alguns testes
[code]import java.util.Vector;
public class Teste {
public static void main(String[] args) {
// Preenchendo a String
String entrada = "SONY Playstation 2 MODEL:39001";
// Declarando e Preenchendo o Vetor
Vector<String> vetor = new Vector<String>();
vetor.add("SONY");
vetor.add("Playstation");
vetor.add("3");
//vetor.add("MODEL:98040"); //Retire o comentário para testar!
// Dividindo a String com uma expressão regular
String[] entradaArray = entrada.split(" ");
// Comparando
int i = 0; // para Incrementar o índice do Vetor
for (String v : entradaArray) {
try {
if (v.equals(vetor.get(i))) {
System.out.println("\"" + v + "\" é igual a \""
+ vetor.get(i) + "\"");
} else {
System.out.println("\"" + v + "\" é diferente de \""
+ vetor.get(i) + "\"");
}
i++; // Incrementa o índice do Vetor
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("\"" + v + "\" não foi comparada!");
System.exit(1);
}
}
}
}
[/code]
valeu pela ajuda pessoal