java中StringTokenizer的用法
StringTokenizer
StringTokenizer可以解析分隔符不是空格的情况。
例子:
import java.util.StringTokenizer;
public class Test {
public static void main(String[] args) {
String a = "i am an engineer";
/*用缺省分隔符空格把a这个字符串分开来, 之后把结果放在StringTokenizer类型的st_Mark_to_win中,即使空很多个格也没问题,这为我们io那章,自己发明自己的j+语言,奠定了坚实的基础*/
StringTokenizer st_Mark_to_win = new StringTokenizer(a);
//返回一共有几个字, 这里是4 个
int count = st_Mark_to_win.countTokens();
for (int i = 0; i < count; i++) {
//nextToken是把下一个字取回来
System.out.println(st_Mark_to_win.nextToken());
}
System.out.println("-------------------------------");
a = "name=lisi;age=26;title=software engineer";
/*用=或者;把a这个字符串分开来, 之后把结果放在StringTokenizer类型的st_Mark_to_win中*/
st_Mark_to_win = new StringTokenizer(a, "=;");
count = st_Mark_to_win.countTokens();
for (int i = 0; i < count; i++) {
System.out.println(st_Mark_to_win.nextToken());
}
}
}
结果:
i
am
an
engineer
-------------------------------
name
lisi
age
26
title
software engineer