Possible Duplicate:
Why does “abcd”.StartsWith(“”) return true?
The following simple Java code just uses the startsWith() method.
package startwithdemo;
final public class Main
{
public static void main(String[] args)
{
System.out.println("My String".startsWith("M"));
System.out.println("My String".startsWith("My"));
System.out.println("My String".startsWith(""));
}
}
It displays true in all the cases. The first two cases are obvious but in the last case (with an empty String), it’s returning true. How?
Because that’s how the API was designed, see the javadoc.
But more seriously, one analogy can be to look at sets. Let’s imagine a string is a set of characters, then the empty string is the empty set. In set theory, the empty set is always part of any set.
Why is the empty set a subset of every set? (taken from here)