【发布时间】:2010-11-06 00:25:34
【问题描述】:
String s1 = "The quick brown fox jumps over the lazy dog";
String s2 = "";
boolean b = s1.contains(s2);
System.out.println(b);
我运行上面的Java代码,b返回true。 既然 s2 是空的,为什么 s1 包含 s2?
我检查了 Java API,它写道:
当且仅当此字符串包含指定的 char 值序列时才返回 true。
参数:
s - 搜索的序列
返回:
如果这个字符串包含s,则为true,否则为false
【问题讨论】:
-
一些关于空字符串的方法:System.out.println("123".contains("")); // true System.out.println("123".startsWith("")); // true System.out.println("123".endsWith("")); // true System.out.println("123".indexOf("")); // 0 System.out.println("123".lastIndexOf("")); // 3
-
一些关于空字符串的方法:
System.out.println("123".contains("")); // true System.out.println("123".startsWith("")); // true System.out.println("123".endsWith("")); // true System.out.println("123".indexOf("")); // 0 System.out.println("123".lastIndexOf("")); // 3