/** *//**
*
* @author bulktree Email: [email protected]
* @date Jul 31, 2008
*/
public class SplitTest {
public static void main(String[] args) {
SplitTest st = new SplitTest();
// st.splitTest01();
//st.splitTest02();
st.StringTokenizierTest();
}
public void splitTest01() {
String s = "01111:aa:哈哈";
/**//*
* limit 该值用来限制返回数组中的元素个数
*/
String[] s1 = s.split(":");
System.out.println(s1[0]);
/**//*
* -1表示如果为空也能区分出来
*/
String[] s2 = s.split(":", -1);
System.out.println(s2[0]);
String[] s3 = s.split(":", 1);
System.out.println(s3[0]);
}
public void splitTest02() {
// “.”和“|”都是转义字符,必须得加"\\"
String s = "bulktree.oakertree.laoshulin";
String ss = "bulktree|oakertree|laoshulin";
String sss = "bulktree.oakertree|laoshulin";
String[] s1 = s.split("\\.");
System.out.println(s1[0]);
String[] s2 = ss.split("\\|");
System.out.println(s2[0]);
/**//*
* 如果有多个分隔符,可以用"|"作为连字符
*/
String[] s3 = sss.split("\\.|\\|");
System.out.println(s3[1]);
}
/**//*
* 构造字符串的StringTokenizer对象测试
*/
public void StringTokenizierTest() {
String s = "I am a student,bulktree";
StringTokenizer str = new StringTokenizer(s);
// 返回nextToken方法被调用的次数,分隔符的数量
System.out.println(str.countTokens());
// 是否有分隔符
while(str.hasMoreTokens()) {
System.out.println(str.nextToken());
}
}
}