Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is my code:

serialNumbers = "";

String[] serialArray = serialNumbers.split(",");

int arrayLength = serialArray.length;

arrayLength is showing 1 even there have no value in serialArray. I was expecting that length should return 0 in this case.

share|improve this question
1  
Consider the case where your string is: ,. How many results would you expect it to return, then? When you answer that, I think you'll understand why this returns 1. –  Colin Morelli Jul 15 '13 at 13:38
 
Or even a non-empty string with no commas. "string". What should that result in? –  Mr Lister Jul 15 '13 at 13:39
add comment

marked as duplicate by sp00m, Andy Thomas, The New Idiot, njzk2, Eran Jul 15 '13 at 14:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

up vote 11 down vote accepted

From the doc:

If the expression does not match any part of the input then the resulting array has just one element, namely this string.

Note that this doc is from the String.split(String, int) method, which is invoked from String.split(String)

share|improve this answer
 
So in this case how I will check if the array is empty or got no value? I need to return if there have value or not. –  ray Jul 15 '13 at 13:34
3  
@ray That will never be the case after using String.split(), so it's a useless test. What's the actual problem you're trying to solve? –  Anthony Grist Jul 15 '13 at 13:36
 
@AnthonyGrist I was suppose to do like: if(arrayLength == 0) { return null; } else { continue doing rest } –  ray Jul 15 '13 at 13:42
 
@ray That's some lines of code, not a problem statement. What's the overall goal of the method that code is in? What are you using String.split to do? What's the purpose behind the check? –  Anthony Grist Jul 15 '13 at 13:47
 
@AnthonyGrist I am receiving a comma separated string from webservice. If the string is empty the function will return null if not it will proceed further. oh I got your point. I can straight away check the comma separated string first. If it's empty then can return null. thanks!! –  ray Jul 15 '13 at 14:01
add comment

Split always returns at least one element.

In the case that a separator is not found, the entire input is returned in a single-element array.

share|improve this answer
 
+1 for explaining reason. –  commit Jul 15 '13 at 13:35
add comment

serialArray contains [""], which is 1 element

share|improve this answer
add comment

If you look at the implementation of String.class (refer snippet below). Here off shows the match count and this is the String currently being processed for split operation and you have ur string as serialNumbers = "";. Thats why it is returning one item in array.

        // If no match was found, return this
        if (off == 0)
            return new String[]{this};
share|improve this answer
add comment

public class TestArgs {

public static void main(String[] args) {
    String checkString = "";

    System.out.println("" + splitString(checkString));



}

public static int splitString(String checkString) {
    if (checkString.indexOf(",") != -1 || !"".equals(checkString)) {
        System.out.println("hello " + checkString.split(",").length);
        return checkString.split(",").length;
    } else {
        return 0;
    }
}

}

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.