java中的split方法

java.lang.string.split

split 方法
将一个字符串分割为子字符串,然后将结果作为字符串数组返回。

stringObj.split([separator,[limit]])

stringObj 
必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。

separator 
可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽
略该选项,返回包含整个字符串的单一元素数组。 

limit
可选项。该值用来限制返回数组中的元素个数。

说明:
split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解
。separator 不作为任何数组元素的部分返回。


示例1:
public class SplitDemo {
    
     public static String[] ss = new String[20];

     public SplitDemo() {

         String s = "The rain in Spain falls mainly in the plain.";
         // 在每个空格字符处进行分解。
         ss = s.split(" ");
     }

     public static void main(String[] args) {

         SplitDemo demo = new SplitDemo();
         for (int i = 0; i < ss.length; i++)
             System.out.println(ss[i]);
     }

}

程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain.


示例2:
public class SplitDemo {

     public static String[] ss = new String[20];

     public SplitDemo() {

         String s = "The rain in Spain falls mainly in the plain.";
         // 在每个空格字符处进行分解。
         ss = s.split(" ", 2);
     }

     public static void main(String[] args) {
         SplitDemo demo = new SplitDemo();
         for (int i = 0; i < ss.length; i++)
             System.out.println(ss[i]);
     }

}

程序结果:
The
rain in Spain falls mainly in the plain.


示例3:
public class SplitDemo {

     public static String[] ss = new String[20];

     public SplitDemo() {

         String s = "The rain in Spain falls mainly in the plain.";
         // 在每个空格字符处进行分解。
         ss = s.split(" ", 20);
     }

     public static void main(String[] args) {
         SplitDemo demo = new SplitDemo();
         for (int i = 0; i < ss.length; i++)
             System.out.println(ss[i]);
     }

}

程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain.


示例4:
public class SplitDemo {

     public static void main(String[] args) {

         String value = "192.168.128.33";
         String[] names = value.split(".");
         for (int i = 0; i < names.length; i++) {
             System.out.println(names[i]);
         }

     }
}

运行结果: 

对,没看错!没有任何输出! 
让我们来看看 split 方法的方法签名吧:

public string[] split(string regex) 
这里的参数的名称是 regex ,也就是 regular expression (正则表达式)。这个参数并不是一个简单的分割用的字符,而是一个正则表达式,看了 split 方法的实现代码就更坚定了我们的信心: 

public string[] split(string regex, int limit) { 
return pattern.compile(regex).split(this, limit); 

split 的实现直接调用的 matcher 类的 split 的方法。读者已经知道,“ . ”在正则表达式中有特殊的含义,因此我们使用的时候必须进行转义。 
只要将
String[] names = value.split(".");
改为
String[] names = value.split("//.");
就可以了。

输出结果:
192
168
128
33 


再加一点儿补充(这是Java帮助文档中的,更清晰一些):

public String[] split(String regex,int limit)根据匹配给定的正则表达式来拆分此字符串。 
此方法返回的数组包含此字符串的每个子字符串,这些子字符串由另一个匹配给定的表达式的子字符串终止或由字符串结束来终止。数组中的子字符串按它们在此字符串中的顺序排列。如果表达式不匹配输入的任何部分,则结果数组只具有一个元素,即此字符串。 

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

例如,字符串 "boo:and:foo" 使用这些参数可生成下列结果: 

Regex      Limit                结果 

   :          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" } 

这种形式的方法调用 str.split(regex, n) 产生与以下表达式完全相同的结果: 

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

参数:
regex - 定界正则表达式
limit - 结果阈值,如上所述 
返回:
字符串数组,根据给定正则表达式的匹配来拆分此字符串,从而生成此数组 
抛出: 
PatternSyntaxException - 如果正则表达式的语法无效
从以下版本开始: 
1.4 


public String[] split(String regex)根据给定的正则表达式的匹配来拆分此字符串。 
该方法的作用就像是使用给定的表达式和限制参数 0 来调用两参数 split 方法。因此,结果数组中不包括结尾空字符串。 

例如,字符串 "boo:and:foo" 产生带有下面这些表达式的结果: 

Regex                 结果 
   :            { "boo", "and", "foo" } 
   o            { "b", "", ":and:f" } 

参数:
regex - 定界正则表达式 
返回:
字符串数组,根据给定正则表达式的匹配来拆分此字符串,从而生成此数组。 
抛出: 
PatternSyntaxException - 如果正则表达式的语法无效 
--------------------- 
作者:daxiang_zhang 
来源:CSDN 
原文:https://blog.csdn.net/daxiang_zhang/article/details/2149896 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/pengzonglu7292/article/details/84782465