为什么字符串以“”开头在Java中?
在调试时通过一些代码,我发现我的验证的一个特定部分是使用 String 类上的 .startsWith()
方法来检查字符串是否以空白字符开头
考虑以下内容:
public static void main(String args[])
{
String s = "Hello";
if (s.startsWith(""))
{
System.out.println("It does");
}
}
它打印出 确实如此
我的问题是,为什么字符串以空白字符开头?我假设字符串本质上是字符数组,但在这种情况下,我认为第一个字符将是 H
谁能解释一下吗?
Possible Duplicate:
Why does “abcd”.StartsWith(“”) return true?
Whilst debugging through some code I found a particular piece of my validation was using the .startsWith()
method on the String class to check if a String started with a blank character
Considering the following :
public static void main(String args[])
{
String s = "Hello";
if (s.startsWith(""))
{
System.out.println("It does");
}
}
It prints out It does
My question is, why do Strings start off with a blank character? I'm presuming that under the hood Strings are essentially character arrays, but in this case I would have thought the first character would be H
Can anyone explain please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
“”是一个不包含任何字符的空字符串。不存在“空字符”,除非您指的是空格或空字符,它们都不是空字符串。
您可以将字符串视为以无限数量的空字符串开头,就像您可以将数字视为以无限数量的前导零开头,而含义没有任何变化。
字符串也以无限数量的空字符串结尾(带零的十进制数也是如此):
"" is an empty string containing no characters. There is no "empty character", unless you mean a space or the null character, neither of which are empty strings.
You can think of a string as starting with an infinite number of empty strings, just like you can think of a number as starting with an infinite number of leading zeros without any change to the meaning.
Strings also end with an infinite number of empty strings (as do decimal numbers with zeros):
您的代码似乎存在误解。您的语句
s.startsWith("")
检查字符串是否以空字符串开头(并且不是空白字符)。这可能是一个奇怪的实现选择,无论如何,它就是这样:所有字符串都会说它们以空字符串开头。另请注意,空白字符将是
" "
字符串,而不是空字符串""
。Seems like there is a misunderstanding in your code. Your statement
s.startsWith("")
checks if string starts with an empty string (and not a blank character). It may be a weird implementation choice, anyway, it's as is : all strings will say you they start with an empty string.Also notice a blank character will be the
" "
string, as opposed to your empty string""
.“Hello”以“”开头,也以“H”开头,也以“He”开头,也以“Hel”开头......你看到了吗?
"Hello" starts with "" and it also starts with "H" and it also starts with "He" and it also sharts with "Hel" ... do you see?
那个“”不是空白,而是一个空字符串。我猜 API 正在问的问题是 this 的子字符串。零长度空字符串是所有内容的子串。
That "" is not a blank it's an empty string. I guess that the API is asking the question is this a substring of that. And the zero-length empty string is a substring of everything.
空字符串(
""
)基本上“满足”每个字符串。在您的示例中,java调用本质
上遵循“空元素(字符串)满足其约束(您的字符串句子)”的原则。
来自 String.java
The empty String (
""
) basically "satisfies" every string. In your example, java callsto
which essentially follows the principle that "an empty element(string) satisfies its constraint (your string sentence).".
From String.java
对于学过自动机理论的人来说,这是有道理的,因为空字符串 ε 是任何字符串的子字符串,也是串联单位元,即:
对于所有字符串 x、ε + x = x 和 x + ε = x
所以是的,每个字符串“startWith”都是空字符串。另请注意(正如许多其他人所说),空字符串与空白或空字符不同。
For folks who have taken automata theory, this makes sense because the empty string ε is a substring of any string and also is the concatenation identity element, ie:
for all strings x, ε + x = x, and x + ε = x
So yes, every string "startWith" the empty string. Also note (as many others said it), the empty string is different from a blank or null character.
空白是(“”),它与空字符串(“”)不同。空格是一个字符,空字符串是没有任何字符。
A blank is (" "), that's different from an empty string (""). A blank space is a character, the empty string is the absence of any character.
空字符串不是空白字符。假设你的问题是空字符串,我猜他们决定保留这种方式,但这看起来确实很奇怪。他们本可以检查长度,但他们没有。
An empty string is not a blank character. Assuming your question with empty string, I guess they decided to leave it that way but it does seem odd. They could have checked the length but they didn't.