Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 214
Dested Avatar asked Sep 28 '08 08:09

Dested


1 Answers

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

like image 164
Jon Skeet Avatar answered Sep 20 '22 15:09

Jon Skeet