String: split()

Jdk的api文档中,描述split方法很详细,但是不仔细研究一下,隐藏的信息是无法显现出来的。

一个实例,分解字符串"boo:and:foo"

package mark.zhang; public class TestSplit { public static void main(String[] args) { String str = "boo:and:foo"; String[] temp = str.split("o",-1); for(int i=0;i<temp.length;i++) { System.out.println(temp[i]); } } }

在这个例子中,调用split(Stringregex)方法,其实等价调用它的过载方法split(Stringregex,intlimit),limint=0,打印结果:

b :and:f //["b","",":and:f"]

分析一下,得到结果的原因,刚开始我以为结果是["b",":and:f"],但是事与愿违。在分析结果之前需要理解这样一句话:

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 如果 n 为 0,那么模式将被应用尽可能多的次数,数组可以是任何长度,并且结尾空字符串将被丢弃。 那么数组最后的所有空字符串都将被丢掉 注意:这里的n就是split(String, int)的int

"boo:and:foo"

第一次用o分隔取得"b" 和 "o:and:foo"

第二次用o分隔取得"" 和 ":and:foo"

第三次用o分隔取得":and:f"和"o"

第四次用o分隔取得"" 和 "" 因为"o"这个分隔了之后可以获取到左右两个空字符串,即忽略掉后面的空字符串

那么,结果是"b" "" ":and:f"

修改上面例子,代码如下:

package mark.zhang; public class TestSplit { public static void main(String[] args) { String str = "boo:and:foo"; String[] temp = str.split("o",-1); for(int i=0;i<temp.length;i++) { System.out.println(temp[i]); } System.out.println("--===--"); } }

运行结果,是这样的:

b :and:f --===-- //["b", "", ":and:f", "", ""]

结合api,分析结果,靠谱一点,一句话:

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. 如果 n 为非正,那么模式将被应用尽可能多的次数,而且数组可以是任何长度

ok,这次没有去掉末尾的两个空字符串。再看一个实例,将n改为正数1,如下:

package mark.zhang; public class TestSplit { public static void main(String[] args) { String str = "boo:and:foo"; String[] temp = str.split("o",1); for(int i=0;i<temp.length;i++) { System.out.println(temp[i]); } System.out.println("--===--"); } }

结果是“boo:and:foo”,yes,没有分解,why???

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 如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后一项将包含所有超出最后匹配的定界符的输入。

因为只执行n-1次 所以1-1 =0 一次都不分隔得到原始字符串。改为其它正数,结果在jdk的api上已经给出:

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

补充一句:limit=9,在此字符串中与limit=5时是一样的效果。

转载一篇:


Java中Split函数的用法技巧

来源:http://www.cnblogs.com/liubiqu/archive/2008/08/14/1267867.html

在java.lang包中也有String.split()方法,与.net的类似,都是返回是一个字符型数组,但使用过程中还有一些小技巧。如执行:

"2|33|4".split("|")

出来的结果是:

"" 2 | 3 3 | 4

奇怪吧,不过注意看一下API说明还是知道原因的。

java.lang.string.split split 方法 将一个字符串分割为子字符串,然后将结果作为字符串数组返回。 stringObj.split([separator,[limit]]) 参数 stringObj 必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。 separator 可选项。字符串或 正则表达式对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽略该选项,返回包含整个字符串的单一元素数组。 limit 可选项。该值用来限制返回数组中的元素个数。 说明 split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解

所以正常的写法是这样的:

1、如果用“.”作为分隔的话,必须是如下写法:String.split("//."),这样才能正确的分隔开,不能用String.split(".");

2、如果用“|”作为分隔的话,必须是如下写法:String.split("//|"),这样才能正确的分隔开,不能用String.split("|");

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

3、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如:“a=1 and b =2 or c=3”,把三个都分隔出来,可以用String.split("and|or");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值