Why does “abcd”.StartsWith(“”) return true?

2019-01-01 15:55发布

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

12条回答
冷夜・残月
2楼-- · 2019-01-01 16:22

Because a string begins well with "nothing".

查看更多
只若初见
3楼-- · 2019-01-01 16:27

I will try to elaborate on what Jon Skeet said.

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

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 "".

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

查看更多
时光乱了年华
4楼-- · 2019-01-01 16:27

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

The empty set is a subset of every set.

Why? The definition of subset states that A is a subset of B if every element of A is an element of B. Conversely, A is not a subset of B if there is an element of A that is not an element of B.

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 B then 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.

Any string starts with the empty string.

First, we must agree on our definition of starts with. Let s and t be strings We say that s starts with t if s.Length >= t.Length and the first t.Length characters of t match those of s. That is, s.Length >= t.Length and for every Int32 index such that 0 <= index < t.Length, s[index] == t[index] is true. Conversely, we would say that s does not start with t if the statement

s.Length < t.Length or s.Length >= t.Length and there is an Int32 index such that 0 <= index < t.Length and s[index] != t[index]

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

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

s.Length < String.Empty.Length or s.Length >= String.Empty.Length and there is an Int32 index such that 0 <= index < String.Empty.Length

is false. Therefore, it is not the case that s does not start with the empty string. Thus, s must start with the empty string.

The following is an implementation of starts with coded as an extension to 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 (subset and starts with) are universal quantifications over 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.

查看更多
不再属于我。
5楼-- · 2019-01-01 16:29

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.

.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 string or is equal to this String object as determined by the equals(Object) method.

Java String.startsWith

查看更多
时光乱了年华
6楼-- · 2019-01-01 16:30

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.Length characters of x match those of y."

An alternative (equivalent) definition:

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

查看更多
梦寄多情
7楼-- · 2019-01-01 16:30

In C# this is how the specification tells it to react;

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

查看更多
登录 后发表回答