为什么字符串以“”开头在Java中?

发布于 2024-09-26 11:56:02 字数 609 浏览 3 评论 0原文

可能的重复:
为什么“abcd”.StartsWith(“”)返回true?

在调试时通过一些代码,我发现我的验证的一个特定部分是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

心意如水 2024-10-03 11:56:02

“”是一个不包含任何字符的空字符串。不存在“空字符”,除非您指的是空格或空字符,它们都不是空字符串。

您可以将字符串视为以无限数量的空字符串开头,就像您可以将数字视为以无限数量的前导零开头,而含义没有任何变化。

1 = ...00001
"foo" = ... + "" + "" + "" + "foo"

字符串也以无限数量的空字符串结尾(带零的十进制数也是如此):

1 = 001.000000...
"foo" = "foo" + "" + "" + "" + ...

"" 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" + "" + "" + "" + ...
情独悲 2024-10-03 11:56:02

您的代码似乎存在误解。您的语句 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 "".

一世旳自豪 2024-10-03 11:56:02

“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?

洒一地阳光 2024-10-03 11:56:02

那个“”不是空白,而是一个空字符串。我猜 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.

云归处 2024-10-03 11:56:02

空字符串("")基本上“满足”每个字符串。在您的示例中,java调用

s.startsWith(""); 

本质

s.startsWith("", 0);

上遵循“空元素(字符串)满足其约束(您的字符串句子)”的原则。

来自 String.java

/**
     * Tests if the substring of this string beginning at the
     * specified index starts with the specified prefix.
     *
     * @param   prefix    the prefix.
     * @param   toffset   where to begin looking in this string.
     * @return  <code>true</code> if the character sequence represented by the
     *          argument is a prefix of the substring of this object starting
     *          at index <code>toffset</code>; <code>false</code> otherwise.
     *          The result is <code>false</code> if <code>toffset</code> is
     *          negative or greater than the length of this
     *          <code>String</code> object; otherwise the result is the same
     *          as the result of the expression
     *          <pre>
     *          this.substring(toffset).startsWith(prefix)
     *          </pre>
     */
    public boolean startsWith(String prefix, int toffset) {
    char ta[] = value;
    int to = offset + toffset;
    char pa[] = prefix.value;
    int po = prefix.offset;
    int pc = prefix.count;
    // Note: toffset might be near -1>>>1.
    if ((toffset < 0) || (toffset > count - pc)) {
        return false;
    }
    while (--pc >= 0) {
        if (ta[to++] != pa[po++]) {
            return false;
        }
    }
    return true;
    } 

The empty String ("") basically "satisfies" every string. In your example, java calls

s.startsWith(""); 

to

s.startsWith("", 0);

which essentially follows the principle that "an empty element(string) satisfies its constraint (your string sentence).".

From String.java

/**
     * Tests if the substring of this string beginning at the
     * specified index starts with the specified prefix.
     *
     * @param   prefix    the prefix.
     * @param   toffset   where to begin looking in this string.
     * @return  <code>true</code> if the character sequence represented by the
     *          argument is a prefix of the substring of this object starting
     *          at index <code>toffset</code>; <code>false</code> otherwise.
     *          The result is <code>false</code> if <code>toffset</code> is
     *          negative or greater than the length of this
     *          <code>String</code> object; otherwise the result is the same
     *          as the result of the expression
     *          <pre>
     *          this.substring(toffset).startsWith(prefix)
     *          </pre>
     */
    public boolean startsWith(String prefix, int toffset) {
    char ta[] = value;
    int to = offset + toffset;
    char pa[] = prefix.value;
    int po = prefix.offset;
    int pc = prefix.count;
    // Note: toffset might be near -1>>>1.
    if ((toffset < 0) || (toffset > count - pc)) {
        return false;
    }
    while (--pc >= 0) {
        if (ta[to++] != pa[po++]) {
            return false;
        }
    }
    return true;
    } 
自由如风 2024-10-03 11:56:02

对于学过自动机理论的人来说,这是有道理的,因为空字符串 ε 是任何字符串的子字符串,也是串联单位元,即:

对于所有字符串 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.

失退 2024-10-03 11:56:02

空白是(“”),它与空字符串(“”)不同。空格是一个字符,空字符串是没有任何字符。

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.

長街聽風 2024-10-03 11:56:02

空字符串不是空白字符。假设你的问题是空字符串,我猜他们决定保留这种方式,但这看起来确实很奇怪。他们本可以检查长度,但他们没有。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文