StringTokenizer类别可以通过某个字符或者多个字符作为分界符,来将字符串划分为多个标记(token)。
package com.example.helloworld; import java.util.StringTokenizer; /** * Created by carl on 10/21/15. */ public class TokenTester { public static void main(String[] arguments) { StringTokenizer st1, st2; String quote1 = "GOOG 604.43 -0.42"; st1 = new StringTokenizer(quote1); int i = 0; for (i = 0; i < 3; i++){ System.out.println(st1.nextToken()); } String quote2 = "hello,java,delphi,asp,php"; st2 = new StringTokenizer(quote2, ","); i = 0; for (i = 0; i < 3; i++){ System.out.println(st2.nextToken()); } } }
运行结果如下:
GOOG
604.43
-0.42
hello
java
delphi