`
xfxlch
  • 浏览: 167532 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

一个String字符串的问题

阅读更多



背景:
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics