Tokenize a string: Difference between revisions

Content added Content deleted
(Added 2 tokenization technique with java)
m (→‎[[Java]]: remove unnessary tabbing)
Line 9: Line 9:
There is multiple way to tokenized a string in Java. The first with a split the String into an array of String, and the other way to give a Enumerator. The second way given here will skip any empty token. So if two commas are given in line, there will be an empty string in the array given by the split function but no empty string with the StringTokenizer object.
There is multiple way to tokenized a string in Java. The first with a split the String into an array of String, and the other way to give a Enumerator. The second way given here will skip any empty token. So if two commas are given in line, there will be an empty string in the array given by the split function but no empty string with the StringTokenizer object.


String toTokenize = "Hello,How,Are,You,Today";
String toTokenize = "Hello,How,Are,You,Today";
//First way
String word[] = toTokenize.split(",");
for(int i=0; i<word.length; i++) {
System.out.println(word[i]);
}
//First way
//Second way
StringTokenizer tokenizer = new StringTokenizer(toTokenize, ",");
String word[] = toTokenize.split(",");
while(tokenizer.hasMoreTokens()) {
for(int i=0; i<word.length; i++) {
System.out.println(word[i]);
System.out.println(tokenizer.nextToken());
}
}
//Second way
StringTokenizer tokenizer = new StringTokenizer(toTokenize, ",");
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}


==[[JavaScript]]==
==[[JavaScript]]==