Java - 浅谈java中的split的用法


Java中的 split  函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回。

(一)一个参数(只包括正则)

 

split
public String[] split(String regex)

Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limitargument of zero. Trailing empty strings are therefore not included inthe resulting array.

The string "boo:and:foo", for example, yields the followingresults with these expressions:

Regex        Result

   :                 { "boo", "and", "foo" } 
Parameters:regex - the delimiting regular expressionReturns:the array of strings computed by splitting this stringaround matches of the given regular expressionThrows。

String str = "1234@Aa";
String[] a = str.split("@");
System.out.println(a[0] + "   " + a[1]);

输出结果:

1234   Aa

但对于一些符号是正则表达式的一部分,需要转义才可以使用。

例如:需要 | 竖线 去分割某字符,因 | 本身是正则表达式的一部分,所以需要\去转义,因为\本身也是正则表达式的字符,所以还需要在使用一个\,所以需要使用两个\\。

String str = "1234|Aa";
String[] a = str.split("\\|");
System.out.println(a[0] + "   " + a[1]);

结果如下:

1234   Aa

这些字符包括:  ,+*^  ,  $  ,  /  ,   , )  ,  -  ,   .   ,   

类似:

//关于\ ,考虑到java转义问题,需要再加一个
String[] a = str.split("\\\\");
//关于* 
String[] a = str.split("\\*");
//关于中括号
String[] a = str.split("\\[\\]");

 

(二)两个参数 

split  public String[] split(String regex,int limit)

这个方法的具体解释分为以下几点:

(1)如果此字符串的开头与结尾存在正宽度匹配,则在结果数组的开头将包含一个空的子字符串。

(2)如果有重复的字符匹配,连续的字符出现,也会出现空的字符串情况。

(3)limit参数会控制匹配的次数。如果该限制 n 大于 0(分成几份),则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后一项将包含所有超出最后匹配的定界符的输入。如果 n 为非正,那么模式将被应用尽可能多的次数,而且数组可以是任何长度。如果 n 为 0,那么模式将被应用尽可能多的次数,数组可以是任何长度,并且结尾空字符串将被丢弃。

接下来,看一下例子:

字符串“ boo:and:foo”使用以下参数产生以下结果:

正则    限制(limit)             结果

:         2                           {“ boo”,“ and:foo”}             (分成2份)

:         5                           {“ boo”,“ and”,“ foo”}        (最多分成3份)
:        -2                           {“ boo”,“ and”,“ foo”}        (负数的效果在没有空字符串的时候跟没有参数的效果是一样的)
o          5                            {“ b”,“”,“:and:f”,“”,“”}      (有重复的字符,出现空的字符串,结尾既重复又在结尾,会出现两个空的字符串,分成了5份)

o          10                            {“ b”,“”,“:and:f”,“”,“”}                   (有重复的字符,出现空的字符串,结尾既重复又在结尾,会出现两个空的字符串,最多就5份)
o         -2                            {“ b”,“”,“:and:f”,“”,“”}      (尽可能多的匹配,会出现空的字符串)
o          0                            {“ b”,“”,“:and:f”}          (尽可能多的匹配,会出现空的字符串,但是结尾的字符串会被丢弃)

特殊情况:

1.空的字符不被解析

public static void main(String[] args)
{ String str
= "1,2,3,4,,,"; String[] arr = str.split(","); for (String string : arr) { System.out.println("str"+string); } System.out.println(arr.length);
}

结果:

 

 2.最后一个分隔符被分的字符串不为空时,其余空字符串可被解析。

public static void main(String[] args)
{
    String str = "1,2,3,4,,,5";
    String[] arr = str.split(",");
    for (String string : arr) {
        System.out.println("str"+string);
    }
    System.out.println(arr.length);
}

结果:

 

 两个参数的总结:

1.当参数为正整数的时候,只需要截取前几个,需要几个截取几个。

 

//输出结果为 6 ,limit参数指定几个,输出几个,最多为 8 个  
String line =  "aa,bb,cc,dd,,,," ;  
System.out.println(line.split( "," , 6 ).length);  

 

2.当参数为零的时候,和split()一样,截图尽可能多的字符串(其实可能不是最多的,结尾的空字符串会被丢弃)。

//输出结果为 4   
String line =  "aa,bb,cc,dd,,,," ;  
System.out.println(line.split( "," , 0 ).length);  

 

3.当参数为负的时候,即使后面有空的串,也会输出到最大 

//输出结果为 8
String line =  "aa,bb,cc,dd,,,," ;  
System.out.println(line.split( "," ,- 1 ).length);  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM