Java中分割字符串的方法--String.split()

一.String[]java.lang.String.split(String regex).

源码注释:Splits this string around matches of the givenregular expression.

 通过查看源码及注释可知,这个方法的参数其实是一个正则表达式,返回的结果则是一个字符类型的数组。 这里的参数的名称是 regex,也就是regular expression(正则表达式)。这个参数并不是一个简单的分割用的字符,而是一个正则表达式,它对一些特殊的字符可能会出现你预想不到的结果,所以这里列举一些在使用split方法分割字符串时要传入的一些特殊字符串。

1、.”和“|”都是转义字符,必须得加"\\";

如果用.”作为分隔的话,必须是如下写法:

String.split("\\."),这样才能正确的分隔开,不能用String.split("."),否则结果为空;

如果用|”作为分隔的话,必须是如下写法:

String.split("\\|"),这样才能正确的分隔开,不能用String.split("|");

String str = "ab|cd|ef|gh";
String resu[] = str.split("|");
for(String k : resu){
	System.out.println(k);
}
结果如图:


String str = "ab|cd|ef|gh";
String resu[] = str.split("\\|");
for(String k : resu){
     System.out.println(k);
}
结果如下:


2、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如String str = "ab4cdef6gh";

String resu[] = str.split("4|6");
for(String k : resu){
System.out.println(k);
}
 

3. 对于字符\,比如字符串String str8 = "abc\defgh";这样的写法是不正确的,正确的写法是String str = "abc\\defgh";此时,使用\\分割字符串时也要进行转义:String resu[] = str.split("\\\\");

4. “$”也要进行转义,否则结果会把包含它自己的整个字符串原样输出;

3. 对于 !  @  #  %  -  &和空格这些符号可以不要进行转义,如果加了转义也不会影响结果。

4. 对与+  *如果不进行转义的话会报错:java.util.regex.PatternSyntaxException

 

二. String[]java.lang.String.split(String regex, int limit)

上面主要介绍了第一个参数的一些特殊情况,下面来看一下第二个参数。

源码的解释如下:

String[] java.lang.String.split(String regex, int limit)

 

Splits this string around matches of the given regular expression.

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

The string "boo:and:foo", for example, yields the following results with these parameters:

 

Regex  Limit  Result

:  2  { "boo", "and:foo" }}

:  5  { "boo", "and", "foo" }}

:  -2  { "boo", "and", "foo" }}

o  5  { "b", "", ":and:f", "", "" }}

o  -2  { "b", "", ":and:f", "", "" }}

o  0  { "b", "", ":and:f" }}

 

An invocation of this method of the form str.split(regex, n) yields the same result as the expression.

java.util.regex.Pattern.compile(regex).split(str, n)  

Parameters:

Regex:  the delimiting regular expression

Limit:  the result threshold, as described above

Returns:the array of strings computed by splitting this string around matches of the given regular expression

Throws:PatternSyntaxException - if the regular expression's syntax is invalid

 

关于注释的翻译网上有很多,但是大多数看了还是不太明白,下面是我实验的一些例子:

举例1

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy");
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图


举例2:                           

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy");
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:



举例3:

String str = "abcxyxyxyxydezxyxyxy";
String result[]  = str.split("xy");
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:


举例4:

String str = "abcxyxyxyxydezxyxyxy";
String result[]  = str.split("xy", 0);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:


总结1:由例子12,34可知,当参数Limit0时,split(String regex)等价于split(String regex, 0),这点由源码也可知晓。如下图


举例5:

String str = "abcxyxyxyxydezxyxyxy";
String result[]  = str.split("xy", 1);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:

举例6:

String str = "abcxyxyxyxydezxyxyxyhhhhh";
String result[]  = str.split("xy", 1);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:

总结2:由例子56可知,当参数limit1时,字符串并没有被分割,结果输出原字符串。


举例7:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy", 2);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:



举例8:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy", 3);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:

举例9:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy", 4);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:


举例10:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy", 5);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:


举例11:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy",7);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:

举例12:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy",9);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:


举例13:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy",19);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:

举例14:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy",-2);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:


举例15:

String str = "abcxyxyxyxydezxyxyxy";
String result[]  = str.split("xy",19);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:

举例16:

String str = "abcxyxyxyxydezxyxyxyfg";
String result[]  = str.split("xy",-2);
for(String i : result){
	System.out.println("i is :  " + i);
}
结果如下图:


好了,试了这么多的例子,我们再回头看看官方注释中关于参数limit的这一段:

The limit parameter controls the number of times the pattern is applied and .......

参考百度翻译,大意为:参数limit控制模式(也就是正则表达式)应用的次数,并且因此会影响产生的结果数组的长度。①如果参数limit的值N大于0,则正则表达式将会被匹配最多N-1次,数组的长度将会不大于N,并且数组的最后一项将包含超出N-1个分隔符后所有的字符串。②如果N是非正的,那么正则表达式将被应用尽可能多的次数,并且结果数组可以有任何长度包括尾随空字符串。如果N为零,那么该正则表达式将尽可能多地应用,数组可以有任何长度,尾随空字符串将被丢弃。


翻译中的第①种情况由例子5——例子13,还有例子15可得到验证。同时也解释了总结2中输出原字符串的情况;第②种情况由例子13——例子16得到验证;第③种情况由总结1得到验证。

 

 总结:

1.当参数limit的值N0时,split(String regex, int limit)等价于split(String regex),正则表达式会在整个字符串中匹配,产生的数组中会抛弃数组结尾的空值;

2.当参数limit的值N大于0且不大于正则表达式在数组中的个数时,正则表达式只会匹配N-1次,数组的长度为N,数组的最后一项中将会包含剩余的regex

3.当参数limit的值N大于正则表达式在数组中的个数+1时,那么正则表达式将被应用尽可能多的次数,并且结果数组可以有任何长度包括尾随空字符串。(其实正则表达式被应用的次数是它在字符串的个数)。

4.当参数limit的值N小于0时,其结果和过程跟3中是一样的。


以上就是个人对于split(String regex, int limit)的学习和理解。


  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值