简体   繁体   English

String.split什么时候返回一个空数组?

[英]When does String.split return an empty array?

My logs show this exception: ArrayIndexOutOfBoundsException: length=0; index=0 我的日志显示此异常: ArrayIndexOutOfBoundsException: length=0; index=0 ArrayIndexOutOfBoundsException: length=0; index=0 triggered by the following piece of code: ArrayIndexOutOfBoundsException: length=0; index=0由以下代码触发:

public static String getInitialsFromFullName(String fullName)
{
    String[] splitNames = fullName.split(" ");
    String firstName = splitNames[0]; <-- Here
    ...
}

I am trying to figure out the condition for which String.split returns an empty array. 我试图弄清楚String.split返回一个空数组的条件。 My understanding is that if no match is found, an array of size 1 with the original string is returned. 我的理解是,如果找不到匹配项,则返回大小为1且包含原始字符串的数组。

This is Java compiled for the Android build SDK version 21. I am looking forward to hear what obvious detail I am missing. 这是针对Android构建SDK版本21编译的Java。我期待听到我遗漏的明显细节。

split(regex) returns result of split(regex,0) where 0 is limit . split(regex)返回split(regex,0)结果,其中0limit Now according to documentation (limit is represented by n ) 现在根据文件 (限制用n表示)

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为零,那么模式将被应用尽可能多的次数,该数组可以具有任何长度,并且将丢弃尾随的空字符串

(emphasis mine) (强调我的)

It means that in case of code like 这意味着在代码的情况下

"ababaa".split("a")

at first you will get array ["", "b","b","",""] but then trailing empty string swill be removed, so you will end up with array ["","b","b"] 首先你会得到数组["", "b","b","",""]然后删除尾随空字符串swill,所以你最终会得到数组["","b","b"]

BUT if your string contains only elements which split can match with its pattern, like 但是,如果您的字符串仅包含split可以与其模式匹配的元素,例如

"ababab".split("ab")

at first array will contain ["","","",""] (three splits), but then empty trailing elements will be removed which means that all elements will be removed, which will leave you with [] (array with size 0). 在第一个数组将包含["","","",""] (三个分裂),但随后将删除空的尾随元素,这意味着将删除所有元素,这将留下[] (数组与大小0)。

So to get empty array as result you need to split on string which contains only substrings which can be matched by split parameter, so in case of split(" ") original string must be build only from spaces, and its length must be at least 1. 因此,要获得空数组作为结果,您需要拆分仅包含可以通过split参数匹配的子串的字符串,因此在split(" ")情况下,原始字符串必须仅由空格构建,并且其长度必须至少为1。

BTW if original string would be empty like "" split(" ") wouldn't happen so array with original string would be returned, which means you will still get [""] array with one element, not empty array. 顺便说一句,如果原始字符串是空的,就像“” split(" ")那样不会发生,所以返回原始字符串的数组,这意味着你仍然会得到一个元素的[""]数组,而不是空数组。

You can say " but you said that trailing empty string are removed, so why this "" is not removed (it is trailing, and it is empty)? ". 你可以说“ 但你说删除尾随空字符串,为什么不删除这个”(它是尾随的,它是空的)? “。 Well yes, but removing trailing empty strings makes sense only if these strings ware created as result of splitting , so if splitting didn't happen "cleanup" is not required, so result array will contain original string as explained in my earlier answer on this subject. 是的,但是删除尾随的空字符串只有在这些字符串由于拆分而​​创建时才有意义,所以如果不发生拆分 ,则不需要“清理”,因此结果数组将包含原始字符串,如我之前的回答中所述学科。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM