菜鸟教程的实例2
7.字符串分割
代码:
public class JavaStringSplitEmp {
public static void main(String[] args) {
String str ="www-runoob-com";
String[] temp;
String delimeter = "-" ;//指定分割字符
temp =str.split(delimeter);//分割字符串
for(int i=0;i<temp.length;i++) {
System.out.println(temp[i]);
System.out.println("");
}
System.out.println("-----java for each循环输出的方法-----");
String str1="www.runoob.com";
String[] temp1;
String delimeter1 = "\\.";//指定分割字符. .号需要转义
temp1=str1.split(delimeter1);//分割字符串
for(String x: temp1) {
System.out.println(x);
System.out.println("");
}
}
}
运行结果:
www
runoob
com
-----java for each循环输出的方法-----
www
runoob
com
8.字符串分隔(String Tokenizer)
代码:
import java.util.StringTokenizer;
public class Stringmain {
public static void main(String[] args) {
String str ="This is String , split by StringTokenizer,created by runoob";
StringTokenizer st = new StringTokenizer(str);
System.out.println("---- 通过空格分隔 ------");
while(st.hasMoreElements()) {
System.out.println(st.nextElement());
}
System.out.println("---- 通过逗号分隔 ------");
StringTokenizer st2 = new StringTokenizer(str,",");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
运行结果:
---- 通过空格分隔 ------
This
is
String
,
split
by
StringTokenizer,created
by
runoob
---- 通过逗号分隔 ------
This is String
split by StringTokenizer
created by runoob