Java String.indexOf 和空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2683466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java String.indexOf and empty Strings
提问by tmeisenh
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.
我很好奇为什么String.indexOf
在要求字符串中空字符串的索引时返回 0(而不是 -1)。
The Javadocs only say this method returns the index in this string of the specified string, -1 if the string isn't found.
Javadocs 只说此方法返回指定字符串在此字符串中的索引,如果未找到该字符串,则返回 -1。
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...
对我来说,这种行为似乎非常出乎意料,我本来期望 -1。任何想法为什么会发生这种意外行为?我至少认为这在方法的 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
采纳答案by Pointy
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.
老师用带子敲打学生,并指示他继续打坐。
回答by T .
Well, if it helps, you can think of "FOO"
as "" + "FOO"
.
好吧,如果有帮助,您可以将其"FOO"
视为"" + "FOO"
.
回答by exoboy
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:
因此,通过说 "".indexOf( "" ),您实际上是在询问解释器:
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.
向字符串中添加任何内容现在将使其成为非空字符串...... null 可以被认为是没有任何东西,甚至什么都没有。
回答by zakmck
Using an algebraic approach, "" is the neutral element of string concatenation: x + "" == x and "" + x == x (although + is non commutative here).
使用代数方法,"" 是字符串连接的中性元素:x + "" == x 和 "" + x == x(尽管 + 在这里是非可交换的)。
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...
当 y = "" 时,如果 i == 0 和 x.substring ( 0, 0 ) == "",则此方法成立。我没有设计Java,但我猜数学家参与了它......
回答by ctomek
int number_of_empty_strings_in_string_named_text = text.length() + 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.
所有字符都由一个空的String
. 此外String
,开头和结尾都存在空。