java StringTokenizer
在java引入正则表达式和Scanner类之前分割字符串的唯一方法是使用StringTokenizer来分词,不过现在有了正则表达式和Scanner类我们可以使用更加简单更加简洁的方式来完成同样的工作了. 下面的例子是它们之间的比较,使用正则表达式和Scanner可以很方便的分割字符StringTokenizer可以废弃不用了
//: strings/ReplacingStringTokenizer.java import java.util.*; public class ReplacingStringTokenizer { public static void main(String[] args) { String input = "But I'm not dead yet! I feel happy!"; StringTokenizer stoke = new StringTokenizer(input); while(stoke.hasMoreElements()) System.out.print(stoke.nextToken() + " "); System.out.println(); System.out.println(Arrays.toString(input.split(" "))); Scanner scanner = new Scanner(input); while(scanner.hasNext()) System.out.print(scanner.next() + " "); } } /* Output: But I'm not dead yet! I feel happy! [But, I'm, not, dead, yet!, I, feel, happy!] But I'm not dead yet! I feel happy! *///:~