IndexOf 方法在 C# / Java 中本应返回 -1 时却返回 0
我的一个朋友带着这种奇怪的行为来找我,我无法解释,任何有洞察力的观点将不胜感激。
我正在运行 VS 2005 (C# 2.0),以下代码显示了
int rr = "test".IndexOf("");
Console.WriteLine(rr.ToString());
上述代码的行为,打印“0”,这清楚地表明它应该返回 -1
这也发生在 Java 中,其中以下类显示了行为:
public class Test{
public static void main(String[] args){
System.out.println("Result->"+("test".indexOf("")));
}
}
我正在运行 Java 1.6。 0_17
A friend of mine came to me with this strange behavior which i can't explain, any insight view would be appreciated.
Im running VS 2005 (C# 2.0), the following code show the behavior
int rr = "test".IndexOf("");
Console.WriteLine(rr.ToString());
the above code, print "0" which clearly show it should have return -1
This also happen in Java where the following Class show the behavior:
public class Test{
public static void main(String[] args){
System.out.println("Result->"+("test".indexOf("")));
}
}
Im running Java 1.6.0_17
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
引用自 C# 文档:
您描述的行为完全符合预期(至少在 C# 中)。
Quote from the C# documentation:
The behavior that you describe is entirely as expected (at least in C#).
0 是正确的。从位置零开始,您可以(简单地)匹配零长度字符串。同样,“”包含“”。
0 is correct. Start at position zero and you can (trivially) match a zero-length string. Likewise, "" contains "".
这并不是规则的例外,而是
indexOf
和startsWith
定义方式的自然结果。您声称
"test".indexOf("")
应该返回-1
。这本质上相当于"test".startsWith("")
应该返回false
的声明。这是为什么呢?尽管这种情况在文档中被明确描述为返回 true,但这不仅仅是一个任意的决定。例如,您将如何决定
"test".startsWith("te")
?最简单的方法是使用递归。由于两个字符串均以字符't'
开头,因此您可以调用"est".startsWith("e")
并返回结果。同样,您将调用"st".startsWith("")
并返回结果。但您已经知道答案应该是true
,因此每个字符串都以""
开头。This is not an exception to the rule, but rather a natural consequence of how
indexOf
andstartsWith
are defined.You’re claiming that
"test".indexOf("")
should return-1
. This is essentially equivalent to the claim that"test".startsWith("")
should returnfalse
. Why is this? Although this case is specifically addressed in the documentation as returningtrue
, 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 betrue
, so that is why every string starts with""
.0 是正确的。 Javadocs 指出
indexOf
的工作原理如下:任何以
""
开头的字符串都等于原始字符串(并且每个字符串都以""
开头),因此最小的kstr = ""
的 code> 始终为 0。0 is correct. The Javadocs point out that
indexOf
works as follows:Any string starting with
""
is equal to the original string (and every string starts with""
), so the smallestk
forstr = ""
is always 0.可以这样想:IndexOf,在查找字符串时,会从位置 0 开始,尝试匹配该字符串,如果不匹配,则移动到位置 1、2 等。当你用空调用它时string,它尝试将空字符串与从位置 0 开始、长度为 0 的字符串进行匹配。万岁,无等于无。
旁注:当您使用 Console.Write/WriteLine 时,没有真正的理由使用
ToString
。该函数自动调用相关对象的ToString
方法。 (除非重载ToString)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 theToString
method of the object in question. (Unless overloading ToString)它应该返回 0。您正在寻找空字符串的第一次出现,对吗? :)
It should return 0. You are looking for the first occurrence of an empty string, right? :)
更有趣的 php 实际上做得更好!
More fun php actually does a way better job!
只是为了好玩。它也像 python 中那样工作,
Python 抛出一个 ValueError 而不是 -1,这很好。
Just for the fun of it. It also works like that in python
Python throws a ValueError instead of the -1 that is nice.