为什么Strings以“"”开头?在Java? [英] Why do Strings start with a "" in Java?
问题描述
虽然通过一些代码调试我发现我验证的一个特定部分是在String类上使用 .startsWith()
方法检查是否以空白字符开头的字符串
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
考虑以下因素:
public static void main(String args[])
{
String s = "Hello";
if (s.startsWith(""))
{
System.out.println("It does");
}
}
打印出它确实
我的问题是,为什么Strings以空白字符开头?我假设在引擎盖下Strings本质上是字符数组,但在这种情况下我会认为第一个字符是 H
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
有人可以解释一下吗?
推荐答案
是一个不包含字符的空字符串。没有空字符,除非你的意思是空格或空字符,它们都不是空字符串。
"" 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.
1 = ...00001
"foo" = ... + "" + "" + "" + "foo"
字符串也以无限数量的空字符串结尾(带有零的十进制数字):
Strings also end with an infinite number of empty strings (as do decimal numbers with zeros):
1 = 001.000000...
"foo" = "foo" + "" + "" + "" + ...
这篇关于为什么Strings以“"”开头?在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!