Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it always safe to use the first element of the array returned by split?

Tags:

java

I'm pretty sure the answer is yes, but I just want to confirm that there is never a situation where a non null string (regardless of what it contains) would return anything but a valid String as the first member of the array returned by split.

In other words.

String foo = ""; // or "something" or "a b c" or any valid string at all

String[] bar = foo.split(",")[0];

My understanding is that bar will never be null and the assignment line has no way to fail. If the delimiter is not found in the string it just returns foo in its entirety as the first element of the returned array.

like image 853
Yevgeny Simkin Avatar asked Mar 16 '12 19:03

Yevgeny Simkin


People also ask

What happens if you split an empty string Java?

The split() method does not change the value of the original string. 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.


4 Answers

No,, It may fail

It would fail to ArrayIndexOutOfBound if the foo =","

like image 60
jmj Avatar answered Sep 18 '22 11:09

jmj


Yes. bar will be equal to the string ""

.split(",") attempts to split after the comma, but there is no comma in the original string, so the original string will get returned.

What would be trickier is:

String s = ",,,,,,,"

String[] sarray = s.split(",");

Here sarray[0] will return ArrayIndexOutOfBoundsException.

like image 35
AdrianS Avatar answered Sep 20 '22 11:09

AdrianS


(1) If foo is a direct match for the regex pattern, the array returned from split has length 0 and foo.split[0] will throw an ArrayIndexOutOfBoundsException.

(2) Keep in mind String.split may throw a PatternSyntaxException if the regex is invalid at runtime.

like image 42
calebds Avatar answered Sep 22 '22 11:09

calebds


Here's a set of test cases for you that demonstrate the above:

public class Test {
    public static void main(String[] args){
        test("x,y");
        test(",y");
        test("");
        test(",");
    }

    private static void test(String x){
        System.out.println("testing split on value ["+x+"]");
        String y = x.split(",")[0];
        if(null == y){
            System.out.println("x returned a null value for first array element");
        } else if(y.length() < 1) {
            System.out.println("x returned an empty string for first array element");
        } else {
            System.out.println("x returned a value for first array element");
        }
    }
}

When run, this is what you get:

$ javac Test.java && java Test
testing split on value [x,y]
x returned a value for first array element
testing split on value [,y]
x returned an empty string for first array element
testing split on value []
x returned an empty string for first array element
testing split on value [,]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at Test.test(Test.java:11)
        at Test.main(Test.java:6)
like image 20
Edward Q. Bridges Avatar answered Sep 18 '22 11:09

Edward Q. Bridges