Java中的String.contains
我运行上面的Java代码,b返回true。 既然s2是空的,为什么s1包含s2 我检查了Java API,它写道: 当且仅当此字符串包含指定的字符值序列时,返回true 参数: s-要搜索的序列 返回: 如果此字符串包含s,则为true;否则为falseJava中的String.contains,java,string,Java,String,我运行上面的Java代码,b返回true。 既然s2是空的,为什么s1包含s2 我检查了Java API,它写道: 当且仅当此字符串包含指定的字符值序列时,返回true 参数: s-要搜索的序列 返回: 如果此字符串包含s,则为true;否则为false String s1 = "The quick brown fox jumps over the lazy dog"; String s2 = ""; boolean b = s1.contains(s2); System.out.println
String s1 = "The quick brown fox jumps over the lazy dog";
String s2 = "";
boolean b = s1.contains(s2);
System.out.println(b);
因此,任何
字符串中都包含空字符串空字符串是任何字符串的子集
把它们看作是介于每两个角色之间的东西
在任何大小的直线上都有无限多的点
(嗯……我想知道如果我用微积分连接无限多个空字符串,会得到什么结果)
请注意,“.equals”(“”)只是一个例子。Java没有给出任何真正的解释(在JavaDoc或许多令人垂涎的代码注释中),但看看代码,这似乎是一个神奇的例子:
调用堆栈:
"".contains(""); // Returns true.
代码:
/**
*由String和StringBuffer共享以执行搜索的代码。这个
*源是正在搜索的字符数组,目标是
*正在搜索的字符串。
*
*@param source正在搜索的字符。
*@param sourceOffset源字符串的偏移量。
*@param sourceCount源字符串的计数。
*@param目标是要搜索的字符。
*@param targetOffset目标字符串的偏移量。
*@param targetCount目标字符串的计数。
*@param fromIndex开始搜索的索引。
*/
静态int indexOf(char[]source,int sourceOffset,int sourceCount,
char[]目标,int targetOffset,int targetCount,
int fromIndex){
if(fromIndex>=sourceCount){
返回(targetCount==0?sourceCount:-1);
}
如果(从索引<0){
fromIndex=0;
}
如果(targetCount==0){//my comment:这是它返回的位置,则
return fromIndex;//传入字符串为0,作为targetCount传入
}//fromIndex也是0,因为搜索从
//源字符串的开头
…//方法的其余部分
对此,显而易见的答案是“JLS就是这么说的。”
思考这是为什么,认为这种行为在某些情况下是有用的。假设你想对一组其他字符串检查字符串,但是其他字符串的数量可以变化。
所以你有这样的东西:
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
* @param targetCount count of the target string.
* @param fromIndex the index to begin searching from.
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {//my comment: this is where it returns, the size of the
return fromIndex; // incoming string is 0, which is passed in as targetCount
} // fromIndex is 0 as well, as the search starts from the
// start of the source string
...//the rest of the method
其中一些是空字符串
如果将空字符串解释为“无输入”,并且此处的目的是确保aString
包含所有“输入”在myStrings
中,空字符串返回false
是误导性的。所有字符串都包含它,因为它什么都不是。如果说它们不包含它,则意味着空字符串中有一些未在字符串中捕获的物质,这是假的。我将使用数学类比来回答您的问题:
在本例中,数字0将不代表任何值。如果您选择一个随机数,例如15,0可以从15中减去多少次?无限次,因为0没有值,因此您无法从15中提取任何值。您是否难以接受15-0=15而不是错误?因此,如果我们将此类比切换回Java编码,字符串“”表示没有值。选择一个随机字符串,说“hello world”,可以从“hello world”中减去多少次“?将字符串看作一组字符,在数学中,空集始终是任何集合的子集。关于空字符串的一些方法: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(“”);//true System.out.println(“123.endsWith(“”);//true System.out.println(“123.indexOf(“”);//0 System.out.println(“123.lastIndexOf(“”);//3实际上不是无限的-可能是字符数+1,因为每个字符前面有一个空字符串,后面有一个空字符串。您不认为在还有一封信?:)再读一遍你的答案,然后意识到你所说的“线”是一条画在纸上的线。实际上,在每一个“线”之间可能有一个元“;-)
for(String s : myStrings) {
check(aString.contains(s));
}
for(String s : myStrings) {
check(aString.contains(s));
}