Java中分隔字符串的方式

package com.wangbiao.split;

import java.util.StringTokenizer;

public class testSplit {
static String str = "diaoyu islands belong to China";

//用Stirng的split()来实现分隔;
public static void String_split(String str0) {
String[] s = new String[10];
s = str0.split(" ");
for (String string : s) {
System.out.println(string);
}

}
    //用StringTokenizer的构造方法来实现分隔;
public static void StringTokenizer_constructor(String str0) {
StringTokenizer st = new StringTokenizer(str0, " ");
while (st.hasMoreElements()) {
String s = (String) st.nextElement();
System.out.println(s);
}

}

public static void String_split2(String str0) {
String[] s = new String[10];
s = str0.split("+");
for (String string : s) {
System.out.println(string);
}
}

public static void StringTokenizer_constructor2(String str0) {
StringTokenizer st = new StringTokenizer(str0, "+");
while (st.hasMoreElements()) {
String s = (String) st.nextElement();
System.out.println(s);
}

}

public static void main(String[] args) {
String_split(str);
StringTokenizer_constructor(str);
//特别说明,如果是用"."和"+"和"*"来分隔的话,要特别注意,"+"和"*"如果是String的split()方法来分隔的话,要转义.而"."要用"[.]来标识;
//对于StirngTokenizer的话,不需要额外标识分隔符
/*String str2 = "diaoyu.islands.belong.to.China";
String str3="diao+yu+islands+belong+to+China";
String_split2(str2);
StringTokenizer_constructor2(str3);*/
}

}

你可能感兴趣的:(Java中分隔字符串的方式)