Tokenize a string: Difference between revisions
Content deleted Content added
No edit summary |
Added 2 tokenization technique with java |
||
Line 1: | Line 1: | ||
{{task}} |
{{task}} |
||
Separate the string "Hello,How,Are,You,Today" by commas into an array so that each index of the array stores a different word. Display the words to the 'user', in the simplest manner possible, separated by a period. To simplify, you may display a trailing period. |
Separate the string "Hello,How,Are,You,Today" by commas into an array so that each index of the array stores a different word. Display the words to the 'user', in the simplest manner possible, separated by a period. To simplify, you may display a trailing period. |
||
==[[Java]]== |
|||
[[Category:Java]] |
|||
'''Compiler:''' JDK 1.0 and up |
|||
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"; |
|||
//First way |
|||
String word[] = toTokenize.split(","); |
|||
for(int i=0; i<word.length; i++) { |
|||
System.out.println(word[i]); |
|||
} |
|||
//Second way |
|||
StringTokenizer tokenizer = new StringTokenizer(toTokenize, ","); |
|||
while(tokenizer.hasMoreTokens()) { |
|||
System.out.println(tokenizer.nextToken()); |
|||
} |
|||
==[[JavaScript]]== |
==[[JavaScript]]== |