45

I'm curious why the String.indexOf is returning a 0 (instead of -1) when asking for the index of an empty string within a string.

The Javadocs only say this method returns the index in this string of the specified string, -1 if the string isn't found.

To me this behavior seems highly unexpected, I would have expected a -1. Any ideas why this unexpected behavior is going on? I would at the least think this is worth a note in the method's Javadocs...

System.out.println("FOO".indexOf("")); // outputs 0 wtf!!!
System.out.println("FOO".indexOf("bar")); // outputs -1 as expected
System.out.println("FOO".indexOf("F")); // outputs 0 as expected
System.out.println("".indexOf("")); // outputs 0 as expected, I think
9
  • 2
    Yes, and what is the question? Commented Apr 21, 2010 at 13:54
  • 1
    "foo".indexOf("") returns 0 and "foo".substring(0.0) returns "". Seems consistent, I'd say. Commented Apr 21, 2010 at 13:55
  • possible duplicate of stackoverflow.com/questions/2568625/… Commented Apr 21, 2010 at 14:15
  • System.out.println("".indexOf("FOO")); <- this does return a -1, I think you might have mixed up what you expected for a result? Commented Apr 21, 2010 at 14:49
  • For something truly mindboggling, "".contains("") is true. So the empty string contains something... and yet it's empty!!! Commented Apr 21, 2010 at 14:51

7 Answers 7

135

The empty string is everywhere, and nowhere. It is within all strings at all times, permeating the essence of their being, yet as you seek it you shall never catch a glimpse.

How many empty strings can you fit at the beginning of a string? Mu

The student said to the teacher,

Teacher, I believe that I have found the nature of the empty string. The empty string is like a particle of dust, and it floats freely through a string as dust floats freely through the room, glistening in a beam of sunlight.

The teacher responded to the student,

Hmm. A fine notion. Now tell me, where is the dust, and where is the sunlight?

The teacher struck the student with a strap and instructed him to continue his meditation.

Sign up to request clarification or add additional context in comments.

3 Comments

But how does one count the number of angels that can dance within the empty string?
this is of course humorous and poetic and what not, but it is not, really, an answer to the question.
@MikeNakis I was much younger in 2010 :)
35

Well, if it helps, you can think of "FOO" as "" + "FOO".

5 Comments

Indeed, the empty string can be found between any two adjacent characters in a string as well as at the start and end. The first such instance is the very start of the string, namely position 0.
I would say empty string occurs zero times in the string Ali G but obviously I'm wrong.
@Ali G: 4 distinct times: At position 0, 1, 2, and 3 as you can easily verify by calling .substring(0,0) up to .substring(3,3) on "FOO".
StringTokenizer st = new StringTokenizer("FOO",""); System.out.println(st.countTokens()); // outputs 1 So it looks like the empty string exists once according to StringTokenizer
@Joachim: However, you can concatenate the empty string any number of times at any position of a string and still get the desired result.
1

int number_of_empty_strings_in_string_named_text = text.length() + 1

All characters are separated by an empty String. Additionally empty String is present at the beginning and at the end.

1 Comment

Now I see that the same description is in Joey's comment, but still I think it's a good idea to put this in a new answer, because that's the only answer that really explains how it works. @Joey if you want to put this answer as your own and delete mine please let me know because you were first with that.
1

This question is actually two questions:

  1. Why should a string contain the empty string?
  2. Why should the empty string be found specifically at index zero?

Answering #1:

It is a matter of convention. The designers of the language could have implemented it either way:

  • The empty string can be found inside all strings
  • The empty string can never be found in any string

They decided that the empty string can be found in all strings so as to be in accordance with Set Theory (See Wikipedia), according to which:

The empty set is a subset of every set including itself.

This also means that even the empty string contains the empty string, and the following statement proves it:

assert "".indexOf( "" ) == 0;

I am not sure why the mathematicians who came up with Set Theory decided that the empty set should behave this way, but I am pretty sure they had their reasons, and it appears that these reasons can be explained in layman's terms, as various youtube videos seem to do; for example: https://www.youtube.com/watch?v=1nBKadtFViM.

Although I have to admit that I have not actually viewed any of those videos, because #AintNoBodyGotNoTimeFoDat.

Answering #2:

The empty string can be found at the first index examined, because the indexOf() function returns the index of the first occurrence, and the empty string is everywhere, including at the first index examined.

3 Comments

Regarding Answer 2. indexOf overloads where the start index is specified as a parameter do not statically return 0 when an empty string is presented as the search token. Instead, it returns the start index. The source code for most major languages has simple indexOf methods that invoke a base implementation of indexOf that accepts all the supported parameters and executes the core logic (for most cases). indexOf is used heavy for other string methods. For example, contains is kind of a "wrapper" around indexOf, so it has the same behaviors discussed in the thread
@KevinBowersox you are right, I changed my wording from "index zero" to "first index examined". (And also gave the answer an overall facelift.)
Within the context of the question, which was about plain indexOf there was nothing wrong with your response. I just elaborated a bit. I really liked the information you shared regarding set theory.
0

Using an algebraic approach, "" is the neutral element of string concatenation: x + "" == x and "" + x == x (although + is non commutative here).

Then it must also be:

x.indexOf ( y ) == i and i != -1 
<==> x.substring ( 0, i ) + y + x.substring ( i + y.length () ) == x

when y = "", this holds if i == 0 and x.substring ( 0, 0 ) == "". I didn't design Java, but I guess mathematicians participated in it...

Comments

0

if we look inside of String implementation for a method "foo".indexOf(""), we arrive at this method:

public int indexOf(String str) {
    byte coder = coder();
    if (coder == str.coder()) {
        return isLatin1() ? StringLatin1.indexOf(value, str.value)
                          : StringUTF16.indexOf(value, str.value);
    }
    if (coder == LATIN1) {  // str.coder == UTF16
        return -1;
    }
    return StringUTF16.indexOfLatin1(value, str.value);
}

If we look inside of any of the called indexOf(value, str.value) methods we find a condition that says:

if the second parameter (string we are searching for) length is 0 return 0:

 public static int indexOf(byte[] value, byte[] str) {
    if (str.length == 0) {
        return 0;
    }
    ...

This is just defensive coding for an edge case, and it is necessary because in the next method that is called to do actual searching by comparing bytes of the string (string is a byte array) it would otherwise have resulted in an ArrayIndexOutOfBounds exception:

public static int indexOf(byte[] value, int valueCount, byte[] str, int strCount, int fromIndex) {
        byte first = str[0];
...

Comments

-1

By using the expression "", you are actually referring to a null string. A null string is an ethereal tag placed on something that exists only to show that there is a lack of anything at this location.

So, by saying "".indexOf( "" ), you are really asking the interpreter:

Where does a string value of null exist in my null string?

It returns a zero, since the null is at the beginning of the non-existent null string.

To add anything to the string would now make it a non-null string... null can be thought of as the absence of everything, even nothing.

3 Comments

So what's the difference between a "null string" and null? Don't we say "empty string" rather than "null string" specifically to avoid confusion with null?
Check out this explanation: stackoverflow.com/questions/4802015/…
The explanation that you linked to (stackoverflow.com/questions/4802015/…) explains the exact opposite of what you claim. This question is tagged as [Java], and in Java, a null string and an empty string are two completely different things. Confusing these two concepts while trying to answer a question on stackoverflow is a grave mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.