import java.util.StringTokenizer;
StringTokenizer s = new StringTokenizer("hello world welcome to Chian!");
System.out.println("Total tokens : " + s.countTokens());
while (s.hasMoreTokens()){
System.out.println("Next token : " + s.nextToken());
}
结果为:
Total tokens : 5
Next token : hello
Next token : world
Next token : welcome
Next token : to
Next token : Chian!
但是如果s=new StringTokenizer("hello world, welcome to Chian!");
结果为:
Total tokens : 5
Next token : hello
Next token : world,
Next token : welcome
Next token : to
Next token : Chian!
分割单词是以空格为基础!