Title is the entire question. Can someone give me a reason why this happens?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- Sorting 3 numbers without branching [closed]
- How to maintain order of key-value in DataFrame sa
- Graphics.DrawImage() - Throws out of memory except
Because a string begins well with "nothing".
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 ""
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 ofB
if every element ofA
is an element ofB
. Conversely,A
is not a subset ofB
if there is an element ofA
that is not an element ofB
.Now fix a set
B
. I'll establish that the empty set is a subset ofB
. I'll do this by showing that it is not the case that the empty set is not a subset ofB
. If the empty set were not a subset ofB
then I could find an element of the empty set that is not inB
. But the empty set does not have any elements and thus I can not find an element that is not inB
. Therefore, it is not the case that the empty set is not a subset ofB
. Thus, the empty set must be a subset ofB
.Any string starts with the empty string.
First, we must agree on our definition of starts with. Let
s
andt
bestring
s We say thats
starts witht
ifs.Length >= t.Length
and the firstt.Length
characters oft
match those ofs
. That is,s.Length >= t.Length
and for everyInt32 index
such that0 <= index < t.Length
,s[index] == t[index]
is true. Conversely, we would say thats
does not start witht
if the statements.Length < t.Length
ors.Length >= t.Length
and there is anInt32 index
such that0 <= index < t.Length
ands[index] != t[index]
is true. In plain English,
s
is shorter thant
, or, if not, there is a character int
not matching the character as the same position ins
.Now fix a string
s
. I'll establish thats
starts with the empty string. I'll do this by showing that it is not the case thats
does not start with the empty string. Ifs
does not start with the empty string thens.Length < String.Empty.Length
ors.Length >= String.Empty.Length
and there is anInt32 index
such that0 <= index < String.Empty.Length
. Buts.Length >= 0
andString.Empty.Length
is equal to zero so it is impossible fors.Length < String.Empty.Length
to be true. Similarly, since ``String.Empty.Lengthis equal to zero, there is no
Int32 indexsatisfying
0 <= index < String.Empty.Length`. Therefores.Length < String.Empty.Length
ors.Length >= String.Empty.Length
and there is anInt32 index
such that0 <= 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
.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.
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
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)
"In C# this is how the specification tells it to react;