import java.util.ArrayList;
public class StringTokenizer {
private static ArrayListtokenizedStr = new ArrayList ();
private static StringBuffer str = new StringBuffer();
public static ArrayListtokenizer(String input){
int strLength = input.length();
return tokenizer(input, 0, strLength);
}
public static ArrayListtokenizer(String input, int currentIndex, int strLength){
if (currentIndex < strLength) {
char curr = input.charAt(currentIndex);
if (curr != ' ') {
str.append(curr);
} else {
if (str.length() != 0) {
tokenizedStr.add(str);
}
str = new StringBuffer();
}
if ((currentIndex == strLength-1 ) && (curr != ' ')) {
tokenizedStr.add(str);
}
currentIndex++;
tokenizer(input, currentIndex, strLength);
}
return tokenizedStr;
}
public static void main(String[] args) {
System.out.println(tokenizer(" Hello World This is Nicholas Key. How are you? "));
}
}
Tuesday, September 22, 2009
Tokenizing String Recursively
This is an update to the blog entry "String tokenizer by hand". In this article, I am demonstrating an example of tokenizing a given string in a recursive manner. The code is as below:
Email the blog author at: nicholaskeytholeong [at] gmail [dot] com
Labels:
Algorithms,
Java,
Recursion
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment