JAVA split 用法

stringObj.split([separator,[limit]]) 根据匹配给定的正则表达式separator来拆分字符串stringObj,拆分limit次。

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

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

limit
可选项。该值用来限制返回数组中的元素个数。
当limit为0时表示尽可能多的次数拆分,不填limit相当于limit为0的用法。

直接用Demo来说明:

package split.lch.test;

public class SplitDemo1 {

	public static String[] sg = new String[50];
	
	public static void main(String[] argf) {
		
		String sb = "One needs three things to be truly happy living in the "
				+ "world: some thing to do, some one to love, some thing to hope for. ";
		
	    // 在每个空格字符处进行分解,无limit参数。
        sg = sb.split(" ");
		System.out.println("使用 sb.split(" ") 的结果如下:");
		for(int i = 0; i < sg.length; i++) {
			System.out.println(sg[i]);
		}
		
	    // 在每个空格字符处进行分解,限制次数0。
        sg = sb.split(" ", 0);
		System.out.println("使用 sb.split(" ", 0) 的结果如下:");
		for(int i = 0; i < sg.length; i++) {
			System.out.println(sg[i]);
		}
		
		 // 在每个空格字符处进行分解,限制次数3。
        sg = sb.split(" ", 3);
		System.out.println("使用 sb.split(" ", 3) 的结果如下:");
		for(int i = 0; i < sg.length; i++) {
			System.out.println(sg[i]);
		}
		
		 // 忽略正则表达式,限制次数50。
        sg = sb.split("", 50);
		System.out.println("使用 sb.split("", 50) 的结果如下:");
		for(int i = 0; i < sg.length; i++) {
			System.out.println(sg[i]);
		}
		
		// 此段代码说明第一个参数是正则表达式
		String value = "192.168.128.33";
		sg = value.split("."); 
		// 此处没有任何输出,“ . ”在正则表达式中有特殊的含义,因此我们使用的时候必须进行转义“\.”。 
		System.out.println("使用 value.split(".") 的结果如下:");
		for(int i = 0; i < sg.length; i++) {
			System.out.println(sg[i]);
		}
		
		sg = value.split("\.");
		System.out.println("使用 value.split("\\.") 的结果如下:");
		for(int i = 0; i < sg.length; i++) {
			System.out.println(sg[i]);
		}
	}
}

 输出结果为:

使用 sb.split(" ") 的结果如下:
One
needs
three
things
to
be
truly
happy
living
in
the
world:
some
thing
to
do,
some
one
to
love,
some
thing
to
hope
for.
使用 sb.split(" ", 0) 的结果如下:
One
needs
three
things
to
be
truly
happy
living
in
the
world:
some
thing
to
do,
some
one
to
love,
some
thing
to
hope
for.
使用 sb.split(" ", 3) 的结果如下:
One
needs
three things to be truly happy living in the world: some thing to do, some one to love, some thing to hope for. 
使用 sb.split("", 50) 的结果如下:

O
n
e
 
n
e
e
d
s
 
t
h
r
e
e
 
t
h
i
n
g
s
 
t
o
 
b
e
 
t
r
u
l
y
 
h
a
p
p
y
 
l
i
v
i
n
g
 
in the world: some thing to do, some one to love, some thing to hope for. 
使用 value.split(".") 的结果如下:
使用 value.split("\.") 的结果如下:
192
168
128
33

 下面再看看文档中的介绍:

split

public String[] split(String regex,
                      int limit)
根据匹配给定的正则表达式来拆分此字符串。

此方法返回的数组包含此字符串的子字符串,每个子字符串都由另一个匹配给定表达式的子字符串终止,或者由此字符串末尾终止。数组中的子字符串按它们在此字符串中出现的顺序排列。如果表达式不匹配输入的任何部分,那么所得数组只具有一个元素,即此字符串。

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

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

RegexLimit结果
: 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
另请参见:
Pattern
  
原文地址:https://www.cnblogs.com/ligui989/p/3423522.html