-2

Can someone help and explain whether indexOf("") returns 0 is predefined same as -1 by returning a negative result?

Thanks and Happy Easter to all!

ps. the following link was helpful, but it does not contain the exact answer to my question "Hello".indexOf("") returns 0

public class ExIndexOf {

    public static void main(String[] args) {

        String s = "We learn Java.";

        System.out.println(s.indexOf("ava"));  // -> 10
        System.out.println(s.indexOf("java")); // -1
        System.out.println(s.indexOf(" ")); // -> 2
        System.out.println(s.indexOf("")); // -> 0
    }
}
Yousaf
  • 27,861
  • 6
  • 44
  • 69
pojna
  • 41
  • 7
  • no, that's not the same as -1. it means that the index of "" is 0. – Stultuske Apr 03 '21 at 10:48
  • Does this answer your question? https://stackoverflow.com/questions/2683466/java-string-indexof-and-empty-strings (it is the duplicate of your linked question). If not: why not? – knittl Apr 03 '21 at 11:06
  • thx buddys! I did try, with no satisfied explanation, hence the question here raised. again thx for the effort and kindness – pojna Apr 03 '21 at 11:18

1 Answers1

1

Because indexOf returns the first position (index) of its argument in the string. Strings in Java, like arrays and collections are zero-indexed, meaning that the index 0 describes the first item. Index 1 is the second item and index n describes the n+1th item. Many functions return the (invalid) index -1 (a "magic" value) to denote "not found" or "error".

The empty string is contained in every string multiple times. The first position where it can be found is at position 0. Think of it as: String s = "" + "We learn Java." (or even more verbose: s = "" + "W" + "" + "e" + "" + " " + "" + "l" + …).

String s = "We learn Java.";

System.out.println(s.indexOf(""));  // -> 0
System.out.println(s.indexOf("W")); // 0
System.out.println(s.indexOf("e")); // -> 1
System.out.println(s.indexOf(" ")); // -> 2
System.out.println(s.indexOf("not found")); // -> -1
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thx knittl for your prompt reply! I come across this question because I wrote while (searchBegin != -1 || searchBegin != 0) { count++; } and it comes to the endless loop and still I do not know why it occurs like this. – pojna Apr 03 '21 at 11:12
  • @pojna the loop is an infinite loop because `searchBegin` is always unequal to -1 OR 0 (and you never update the loop variable). If `searchBegin == -1` then it is `!= 0`. If `searchBegin == 0`, then it is `!= -1`. If `searchBegin = 42`, then it is `!= -1` and also `!= 0`. Your condition is _always_ true, regardless of value (a "tautology"). – knittl Apr 03 '21 at 11:29
  • @pojna the condition can be transformed into the equivalent form `!(searchBegin == -1 && searchBegin == 0)` – do you see the error when written in this way? A variable can never be two distinct values at the same time. – knittl Apr 03 '21 at 11:34
  • Thx knittl for your valuable comments! Sorry for my primitive questions. Yes, you are right as I changed it to &&. As I got no result feeded back to while (searchBegin != 0), I did doubt how indexOf("") reacts internally. Now I gradually come to the clue and thanks again for your help! Nice holidays! – pojna Apr 03 '21 at 11:48
  • BTW, is it really the case s = "" + "W" + "" + "e" + "" + " " + "" + "l" + …+""? (Does it mean at the end of the string there's also a "" attached?) I checked the following and found last position of "" and string length are exactly the same. String s1 = "We learn Java."; String s2 = ""; int lastInd = s1.lastIndexOf(s2); System.out.println(lastInd); // 14 System.out.println(s1.length()); // 14 – pojna Apr 03 '21 at 12:11
  • @pojna since the empty string is "nothing" it "appears" everywhere in a string. In the beginning, between letters, at the end. See this answer in the linked question https://stackoverflow.com/a/2683509/112968. Compare the empty string to the value 0 when adding values. You can always add 0 and the result will not change. 3 = 3 = 0 + 3 + 0 = 0 + 1 + 0 + 1 + 0 + 1 + 0 = 2 + 0 + 0 + 0 + 1 – knittl Apr 03 '21 at 15:29
  • Thank you very much! Nice holiday! – pojna Apr 03 '21 at 22:41