Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does String.split return an empty array?

Tags:

java

android

My logs show this exception: ArrayIndexOutOfBoundsException: length=0; index=0 triggered by the following piece of code:

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. My understanding is that if no match is found, an array of size 1 with the original string is returned.

This is Java compiled for the Android build SDK version 21. I am looking forward to hear what obvious detail I am missing.

like image 698
siger Avatar asked Jan 20 '15 00:01

siger


People also ask

Can string split return empty array?

If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.

What does an empty string array return?

If the array has a length of zero, then it does not contain any element. To return an empty array from a function, we can create a new array with a zero size. In the example below, we create a function returnEmptyArray() that returns an array of int . We return new int[0] that is an empty array of int .

What does split return if not found?

split (separator, limit) , if the separator is not in the string, it returns a one-element array with the original string in it.

What is the return type of split?

The return type of Split is an Array of type Strings.


1 Answers

split(regex) returns result of split(regex,0) where 0 is limit. Now according to documentation (limit is represented by 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.

(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"]

BUT if your string contains only elements which split can match with its pattern, like

"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).

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.

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.

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.

like image 130
Pshemo Avatar answered Oct 07 '22 19:10

Pshemo