C# 为什么 "abcd".StartsWith("") 返回 true?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/145509/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:32:02  来源:igfitidea点击:

Why does "abcd".StartsWith("") return true?

提问by Dested

Title is the entire question. Can someone give me a reason why this happens?

标题是整个问题。有人可以给我一个为什么会发生这种情况的原因吗?

采纳答案by Jon Skeet

Yes - because it does begin with the empty string. Indeed, the empty string logically occurs between every pair of characters.

是的 - 因为它确实以空字符串开头。实际上,空字符串在逻辑上出现在每对字符之间。

Put it this way: what definition of "starts with" could you give that would preclude this? Here's a simple definition of "starts with" that doesn't:

这么说吧:你能给出什么“开始于”的定义来排除这一点?这是“开头”的一个简单定义,但不包含:

"x starts with y if the first y.Lengthcharacters of x match those of y."

“如果 x 的第一个y.Length字符与 y 的第一个字符匹配,则 x以 y 开头。”

An alternative (equivalent) definition:

替代(等效)定义:

"x starts with y if x.Substring(0, y.Length).Equals(y)"

“x 以 y 开头,如果x.Substring(0, y.Length).Equals(y)

回答by Firas Assaad

This method compares the value parameter to the substring at the beginning of this string that is the same length as value, and returns a value that indicates whether they are equal. To be equal, value must be an empty string (Empty), a reference to this same instance, or match the beginning of this instance.

此方法将 value 参数与此字符串开头的与 value 长度相同的子字符串进行比较,并返回一个值来指示它们是否相等。要相等,value 必须是空字符串 (Empty)、对同一实例的引用或匹配此实例的开头。

.NET String.StartsWith

.NET String.StartsWith

true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty stringor is equal to this String object as determined by the equals(Object) method.

如果参数所表示的字符序列是该字符串所表示的字符序列的前缀,则为真;否则为假。另请注意,如果参数是空字符串或等于由 equals(Object) 方法确定的此 String 对象,则将返回 true

Java String.startsWith

Java String.startsWith

回答by Ralph

The first N characters of the two strings are identical. N being the length of the second string, i.e. zero.

两个字符串的前 N ​​个字符是相同的。N 是第二个字符串的长度,即零。

回答by blowdart

In C# this is how the specificationtells it to react;

在 C# 中,这是规范告诉它做出反应的方式;

To be equal, value must be an empty string (Empty), a reference to this same instance, or match the beginning of this instance.

要相等,value 必须是空字符串 (Empty)、对同一实例的引用或匹配此实例的开头。

回答by gizmo

Because a string begins well with "nothing".

因为字符串以“无”开头。

回答by gizmo

If you think of it in regular expressions terms, it makes sense. Every string (not just "abcd", also "" and "sdf\nff") , returns true when evaluating the regular expression of 'starts with empty string'.

如果你用正则表达式来考虑它,这是有道理的。每个字符串(不仅是 "abcd",还有 "" 和 "sdf\nff"),在评估“以空字符串开头”的正则表达式时都返回 true。

回答by pero

I will try to elaborate on what Jon Skeet said.

我将尝试详细说明 Jon Skeet 所说的内容。

Let's say x, y and z are strings and + operator is in fact concatenation, then:

假设 x、y 和 z 是字符串,而 + 运算符实际上是连接,那么:

If we can split z to write z = x + y that means that z starts with x. Because every string z can be split to z = "" + z it follows that every string starts with "".

如果我们可以将 z 拆分为 z = x + y,则意味着 z 以 x 开头。因为每个字符串 z 都可以拆分为 z = "" + z,所以每个字符串都以 "" 开头。

So, because ("" + "abcd") == "abcd" it follows that "abcd" starts with ""

所以,因为 ("" + "abcd") == "abcd" 所以 "abcd" 以 "" 开头

回答by Pop Catalin

Let's just say "abcd".StartsWith("")returns false.

让我们说"abcd".StartsWith("")返回false。

if so then what does the following expression eval to, true or false:

如果是,那么以下表达式的结果是真还是假:

 ("abcd".Substring(0,0) == "")

it turns out that evals to true, so the string does start with the empty string ;-), or put in other words, the substring of "abcd" starting at position 0 and having 0 length equals the empty string "". Pretty logical imo.

事实证明,evals 为真,因此字符串确实以空字符串开头 ;-),或者换句话说,“abcd”的子字符串从位置 0 开始并且长度为 0 等于空字符串“”。非常合乎逻辑的imo。

回答by jason

I'll start with a related fact that is easier to understand.

我将从一个更容易理解的相关事实开始。

The empty set is a subset of every set.

空集是每个集的子集。

Why? The definitionof subsetstates that Ais a subset of Bif every element of Ais an element of B. Conversely, Ais not a subset of Bif there is an element of Athat is not an element of B.

为什么?该定义子集指出A的一个子集B,如果每一个元素A是一个元素B。相反,如果有一个元素不是 的元素,A则不是 的子集。BAB

Now fix a set B. I'll establish that the empty set is a subset of B. I'll do this by showing that it is not the case that the empty set is not a subset of B. If the empty set were not a subset of Bthen I could find an element of the empty set that is not in B. But the empty set does not have any elements and thus I can not find an element that is not in B. Therefore, it is not the case that the empty set is not a subset of B. Thus, the empty set must be a subset of B.

现在修复一个集合B。我将确定空集是 的子集B。我将通过证明空集不是B. 如果空集不是 的子集,B那么我可以找到不在B. 但是空集没有任何元素,因此我找不到不在B. 因此,空集不是 的子集的情况并非如此B。因此,空集必须是 的子集B

Any string starts with the empty string.

任何字符串都以空字符串开头。

First, we must agree on our definition of starts with. Let sand tbe strings We say that sstarts withtif s.Length >= t.Lengthand the first t.Lengthcharacters of tmatch those of s. That is, s.Length >= t.Lengthand for every Int32 indexsuch that 0 <= index < t.Length, s[index] == t[index]is true. Conversely, we would say that sdoes not start with tif the statement

首先,我们必须就以开头的定义达成一致。Letstbe strings 我们说stif开头,s.Length >= t.Length并且匹配的第一个t.Length字符。也就是说,对于每个这样的,都是真的。相反,我们会说if 语句不以if开头tss.Length >= t.LengthInt32 index0 <= index < t.Lengths[index] == t[index]st

s.Length < t.Lengthor s.Length >= t.Lengthand there is an Int32 indexsuch that 0 <= index < t.Lengthand s[index] != t[index]

s.Length < t.Length或者s.Length >= t.Length有一个Int32 index这样的0 <= index < t.Lengths[index] != t[index]

is true. In plain English, sis shorter than t, or, if not, there is a character in tnot matching the character as the same position in s.

是真的。通俗地说,s比 短t,或者,如果不是,则有一个字符 ints. 中相同位置的字符不匹配。

Now fix a string s. I'll establish that sstarts with the empty string. I'll do this by showing that it is not the case that sdoes not start with the empty string. If sdoes not start with the empty string then s.Length < String.Empty.Lengthor s.Length >= String.Empty.Lengthand there is an Int32 indexsuch that 0 <= index < String.Empty.Length. But s.Length >= 0and String.Empty.Lengthis equal to zero so it is impossible for s.Length < String.Empty.Lengthto be true. Similarly, since ``String.Empty.Lengthis equal to zero, there is noInt32 indexsatisfying0 <= index < String.Empty.Length`. Therefore

现在修复一个字符串s。我将确定s以空字符串开头。我将通过展示s不以空字符串开头的情况来做到这一点。如果s不以空字符串开头,则s.Length < String.Empty.Lengthors.Length >= String.Empty.Length并且存在Int32 index这样的0 <= index < String.Empty.Length。但是s.Length >= 0andString.Empty.Length等于零,所以不可能为s.Length < String.Empty.Length真。同样,由于``String.Empty.Length is equal to zero, there is noInt32 index satisfying0 <= index < String.Empty.Length`。所以

s.Length < String.Empty.Lengthor s.Length >= String.Empty.Lengthand there is an Int32 indexsuch that 0 <= index < String.Empty.Length

s.Length < String.Empty.Length或者s.Length >= String.Empty.Length有一个Int32 index这样的0 <= index < String.Empty.Length

is false. Therefore, it is not the case that sdoes not start with the empty string. Thus, smust start with the empty string.

是假的。因此,s不以空字符串开头的情况并非如此。因此,s必须以空字符串开头。

The following is an implementation of starts withcoded as an extension to string.

以下是将开始编码为string.

public static bool DoStartsWith(this string s, string t) {
    if (s.Length >= t.Length) {
        for (int index = 0; index < t.Length; index++) {
            if (s[index] != t[index]) {
                return false;
            }
        }
        return true;
    }
    return false;
}

The above two bolded facts are examples of vacuously true statements. They are true by virtue of the fact that the statements defining them (subsetand starts with) are universal quantificationsover empty universes. There are no elements in the empty set, so there can not be any elements of the empty set not in some other fixed set. There are no characters in the empty string, so there can not be a character as some position in the empty string not matching the character in the same position in some other fixed string.

以上两个加粗的事实是空洞真实的陈述的例子。它们是真实的,因为定义它们的陈述(子集以 开头)是对空宇宙的普遍量化。空集中没有元素,因此空集的任何元素都不能存在于其他固定集中。空字符串中没有字符,因此不能有一个字符,因为空字符串中的某个位置与其他固定字符串中相同位置的字符不匹配。

回答by Tamas Czinege

Just for the record, String.StartsWith()internally calls the method System.Globalization.CultureInfo.IsPrefix()which makes the following check explicitly:

只是为了记录,String.StartsWith()内部调用System.Globalization.CultureInfo.IsPrefix()显式进行以下检查的方法:

if (prefix.Length == 0)
{
    return true;
}