背景:
String字符串是在Java中用的最多的一个数据类型,关于split的操作也是很频繁的。
遇到一个问题,就是当我用String.split方法的时候,当我的字符串是空的时候,不管split是用的哪个字符串,都会返回一个含有一个空字符串的String数组。
System.out.println("String length: " + "".split(",").length);
System.out.println("String lengthaa: " + "aa".split(",").length);
System.out.println("String lengthaa,: " + "aa,".split(",").length);
Result:
String length: 1
String lengthaa: 1
String lengthaa,: 1
分析:
很是奇怪就去看了jdk docs。
引用
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
就是说spilt(String regex)这个方法是通过调用另外一个有两个参数的split方法起作用的,两个参数里:一个是传入的regex;另一个就是值为0的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.
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.
If the expression does not match any part of the input then the resulting array has just one element, namely this string这句话,解读了我最开始的疑问。也就是说,如果没有匹配到的话,就把String字符串本身作为一个element放到数组中去。也就是上文中,得到的值是:
for(String a:"".split(",")) System.out.println("String length1: " + a);
for(String a:"aa".split(",")) System.out.println("String length2: " + a);
for(String a:"aa,".split(",")) System.out.println("String length3: " + a);
Result
String length1:
String length2: aa
String length3: aa
这里为什么第二个和第三个是一样的结果呢:因为:
引用
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
也就是说原来"aa,"字符串split出来的是两个的,但是由于第二个值是空字符串"",后面又没有跟什么其他的值,那么最后的empty string 将会被丢弃。
但是如果最后面还有一个“ ”white space的话就不会被留下。
例如:
System.out.println("String lengthaa,: " + "aa, ".split(",").length);
for(String a:"aa, ".split(",")) System.out.println("String length3: " + a);
Result:
String lengthaa,: 2
String length3: aa
String length3:
这样子好理解了吧。
也就是说即使是这样的"aa,,,,,,,"一个字符串,结果还是就一个值aa。 大家可以去测试看看。
解答:
最后这里有一道题目:
引用
Consider the following program and predict the output:
class Test {
public static void main(String args[]) {
String test = "I am preparing for OCPJP";
String[] tokens = test.split("\\S");
System.out.println(tokens.length);
}
}
a) 0
b) 5
c) 12
d) 16
那么请问,答案是哪个呢?
答案就是QRCODE
----EOF----
- 大小: 435 Bytes
分享到:
相关推荐
string常用截取字符串方法有很多,但是配合使用以下两种,基本都能满足要求: find(string strSub, npos); find_last_of(string strSub, npos); 其中strSub是需要寻找的子字符串,npos为查找起始位置。找到返回子...
Base64转String字符串,支持将Base64转化为String字符串
基于Keil实现字符串函数string.h的简单应用基于Keil实现字符串函数string.h的简单应用基于Keil实现字符串函数string.h的简单应用基于Keil实现字符串函数string.h的简单应用基于Keil实现字符串函数string.h的简单应用...
C++自定义string字符串类,包含字符串的常用操作以及IO流操作
用string拆分字符串 用string拆分字符串 用string拆分字符串
这个代码,可以删除一个字符串中你想删除的字符或字符串
string字符串含有大写字母将其中大写字母转为小写字母,代码能成功运行。
Android String字符串数组动态增加Android String字符串数组动态增加Android String字符串数组动态增加Android String字符串数组动态增加
305-字符串函数string.h应用举例(51单片机C语言实例Proteus仿真和代码)305-字符串函数string.h应用举例(51单片机C语言实例Proteus仿真和代码)305-字符串函数string.h应用举例(51单片机C语言实例Proteus仿真和代码)...
必须实现如下操作,字符串比较、求串的长度、判断串是否为空、将串置空、字符串赋值(包括两个字符串类复制,一个字符串赋值到CmyString对象)、求字符串中的一个字符或改变字符串中的一个字符(采用重载[]),完成...
java的String字符串总有补0,绝对好用
MidMid(string,start,length)从string字符串的start字符开始取得length长度的字符串,如果省略第三个参数表示从start字符开始到字符串结尾的字符串 LeftLeft(string,length)从string字符串的左边取得length长度的...
C语言实现String字符串及其函数,仅供参考~ C语言实现String字符串及其函数,仅供参考~ C语言实现String字符串及其函数,仅供参考~
Mid Mid(string,start,length) 从string字符串的start字符开始取得length长度的字符串,如果省略第三个参数表示从start字符开始到字符串结尾的字符串 Left Left(string,length) 从string字符串的左边取得length长度...
创建String字符串 目录 课程导入 掌握String字符串对象的创建 掌握字符串对象的输入与输出 一般程序需要处理大量文本数据Java语言的文本数据被保存为字符或字符串类型。 若干个字符在计算机里面如何存储? 如何引用...
可以自动帮你把字符串以单引号分隔,方便sql中in查询
String字符串生成图片,输出一个Image类型的图片。
java字符串操作常用实例,String 字符串操作、比较、查找字符、替换、反转、分割等
字符串数组转换成string类型的
C#字符串删除指定字符串|C#字符串删除子字符串