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
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'.
In C#, the reason it returns
true
is that the developers specifically coded for it.If you check out the source code, you'll find specific logic to handle an empty string:
THE REAL ANSWER:
It has to be that way otherwise you'd have the case where
and then we'd have Y2K all over again because all the bank software that depends on equal strings starting with themselves will get our accounts mixed up and suddenly Bill Gates will have my wealth and I'd have his, and damn it! Fate just isn't that kind to me.
Just for the record,
String.StartsWith()
internally calls the methodSystem.Globalization.CultureInfo.IsPrefix()
which makes the following check explicitly:The first N characters of the two strings are identical. N being the length of the second string, i.e. zero.
Let's just say
"abcd".StartsWith("")
returns false.if so then what does the following expression eval to, true or false:
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.