IndexOf method returns 0 when it should had return -1 in C# / Java

醉酒当歌 提交于 2019-12-01 04:14:14

This is not an exception to the rule, but rather a natural consequence of how indexOf and startsWith are defined.

You’re claiming that "test".indexOf("") should return -1. This is essentially equivalent to the claim that "test".startsWith("") should return false. Why is this? Although this case is specifically addressed in the documentation as returning true, this is not just an arbitrary decision.

How would you decide "test".startsWith("te"), for example? The simplest way is to use recursion. Since both strings start with the character 't', you call "est".startsWith("e") and return the result. Similarly, you will call "st".startsWith("") and return the result. But you already know that the answer should be true, so that is why every string starts with "".

Quote from the C# documentation:

If value is Empty, the return value is 0.

The behavior that you describe is entirely as expected (at least in C#).

0 is correct. Start at position zero and you can (trivially) match a zero-length string. Likewise, "" contains "".

0 is correct. The Javadocs point out that indexOf works as follows:

The integer returned is the smallest value k such that:

 this.startsWith(str, k)

Any string starting with "" is equal to the original string (and every string starts with ""), so the smallest k for str = "" is always 0.

Think of it this way: IndexOf, when looking for a string, will start at position 0, try to match the string, if it doesn't fit, move on to position 1, 2, etc. When you call it with an empty string, it attempts to match the empty string with the string starting at position 0 with length 0. And hooray, nothing equals nothing.

Side note: There's no real reason to use ToString when you're using Console.Write/WriteLine. The function automatically calls the ToString method of the object in question. (Unless overloading ToString)

It should return 0. You are looking for the first occurrence of an empty string, right? :)

More fun php actually does a way better job!

php -r "print strpos('test','');"
PHP Warning:  strpos(): Empty delimiter. in Command line code on line 1

Just for the fun of it. It also works like that in python

>>> "test".startswith("")
True
>>> "test".index("")
0

Python throws a ValueError instead of the -1 that is nice.

>>> "test".index('r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!