Java中String类的split()方法详解
作者:互联网
java split()方法:
- 语法
public String[] split(String str,int limit)
参数str:正则表达式分隔符
参数limit:分割份数
因此,该方法就是用来根据匹配给定的正则表达式来拆分字符串
注意: . 、 $、 | 和 * 等转义字符,必须得加 \,且多个分隔符,可以用 | 作为连字符。 - 实例
public class Test {
public static void main(String args[]) {
String str = new String("Welcome-to-Runoob");
System.out.println("- 分隔符返回值 :" );
for (String retval: str.split("-")){
System.out.println(retval);
}
System.out.println("");
System.out.println("- 分隔符设置分割份数返回值 :" );
for (String retval: str.split("-", 2)){
System.out.println(retval);
}
System.out.println("");
String str2 = new String("www.runoob.com");
System.out.println("转义字符返回值 :" );
for (String retval: str2.split("\\.", 3)){
System.out.println(retval);
}
System.out.println("");
String str3 = new String("acount=? and uu =? or n=?");
System.out.println("多个分隔符返回值 :" );
for (String retval: str3.split("and|or")){
System.out.println(retval);
}
}
}
- 结果
- 分隔符返回值 :
Welcome
to
Runoob
- 分隔符设置分割份数返回值 :
Welcome
to-Runoob
转义字符返回值 :
www
runoob
com
多个分隔符返回值 :
acount=?
uu =?
n=?
标签:Java,String,System,split,分隔符,println,retval,out 来源: https://www.cnblogs.com/feixiong1/p/14600746.html