String.split的混乱输出 [英] Confusing output from String.split
本文介绍了String.split的混乱输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白这段代码的输出:
I do not understand the output of this code:
public class StringDemo{
public static void main(String args[]) {
String blank = "";
String comma = ",";
System.out.println("Output1: "+blank.split(",").length);
System.out.println("Output2: "+comma.split(",").length);
}
}
得到以下输出:
Output1: 1
Output2: 0
$ b b
推荐答案
文档:
For: System.out.println Output1:+ blank.split(,)。length);
方法包含此字符串的每个子字符串,该子字符串由与给定表达式匹配或由字符串结尾终止的另一个子字符串终止。数组中的子字符串是它们在此字符串中出现的顺序。 如果表达式与输入的任何部分不匹配,则结果数组只有一个元素,即。
它将简单地返回整个字符串,这就是它返回1的原因。
It will simply return the entire string that's why it returns 1.
对于第二种情况, String.split
将舍弃,
,因此结果将为空。
For the second case, String.split
will discard the ,
so the result will be empty.
String.split silently discards trailing separators
a href =https://code.google.com/p/guava-libraries/wiki/StringsExplained#Splitter> guava StringsExplained
see guava StringsExplained too
这篇关于String.split的混乱输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文