Java 中的 String.contains

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1046809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:43:12  来源:igfitidea点击:

String.contains in Java

javastring

提问by

String s1 = "The quick brown fox jumps over the lazy dog";
String s2 = "";
boolean b = s1.contains(s2);
System.out.println(b);

I run the Java code above, the b return true. Since s2 is empty, why does s1 contains s2?

我运行上面的Java代码,b返回t​​rue。既然 s2 是空的,为什么 s1 包含 s2?

I check the Java API, it write:

我检查了 Java API,它写道:

Returns true if and only if this string contains the specified sequence of char values.

当且仅当此字符串包含指定的 char 值序列时才返回 true。

Parameters:

参数:

s - the sequence to search for

s - 要搜索的序列

Returns:

返回:

true if this string contains s, false otherwise

如果此字符串包含 s,则为 true,否则为 false

采纳答案by Bill K

Empty is a subset of any string.

Empty 是任何字符串的子集。

Think of them as what is between every two characters.

将它们视为每两个字符之间的内容。

Kind of the way there are an infinite number of points on any sized line...

在任何大小的线上都有无数点的方式......

(Hmm... I wonder what I would get if I used calculus to concatenate an infinite number of empty strings)

(嗯……我想知道如果我使用微积分连接无限数量的空字符串会得到什么)

Note that "".equals("") only though.

请注意,只有 "".equals("") 。

回答by coobird

Similarly:

相似地:

"".contains("");     // Returns true.

Therefore, it appears that an empty string is contained in any String.

因此,在 any 中似乎包含一个空字符串String

回答by akf

no real explanation is given by Java (in either JavaDoc or much coveted code comments), but looking at the code, it seems that this is magic:

Java 没有给出真正的解释(在 JavaDoc 或许多令人垂涎的代码注释中),但是查看代码,这似乎很神奇:

calling stack:

调用栈:

String.indexOf(char[], int, int, char[], int, int, int) line: 1591  
String.indexOf(String, int) line: 1564  
String.indexOf(String) line: 1546   
String.contains(CharSequence) line: 1934    

code:

代码:

/**
 * 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 

回答by patrick.kasarski

The obvious answer to this is "that's what the JLS says."

对此,显而易见的答案是“这就是 JLS 所说的”。

Thinking about why that is, consider that this behavior can be useful in certain cases. Let's say you want to check a string against a set of other strings, but the number of other strings can vary.

考虑为什么会这样,请考虑此行为在某些情况下可能有用。假设您想根据一组其他字符串检查一个字符串,但其他字符串的数量可能会有所不同。

So you have something like this:

所以你有这样的事情:

for(String s : myStrings) {
   check(aString.contains(s));
}

where some s's are empty strings.

其中 somes是空字符串。

If the empty string is interpreted as "no input," and if your purpose here is ensure that aStringcontains all the "inputs" in myStrings, then it is misleading for the empty string to return false. All strings contain it because it is nothing. To say they didn't contain it would imply that the empty string had some substance that was not captured in the string, which is false.

如果空字符串被解释为“无输入”,并且您在这里的目的是确保aString包含 中的所有“输入” myStrings,那么将空字符串返回给误导false。所有字符串都包含它,因为它什么都不是。说它们不包含它意味着空字符串包含一些未在字符串中捕获的内容,这是错误的。

回答by Locke

I will answer your question using a math analogy:

我将使用数学类比来回答您的问题:

In this instance, the number 0 will represent no value. If you pick a random number, say 15, how many times can 0 be subtracted from 15? Infinite times because 0 has no value, thus you are taking nothing out of 15. Do you have difficulty accepting that 15 - 0 = 15 instead of ERROR? So if we switch this analogy back to Java coding, the String "" represents no value. Pick a random string, say "hello world", how many times can "" be subtracted from "hello world"?

在这种情况下,数字 0 将代表没有值。如果你选择一个随机数,比如 15,从 15 中可以减去多少次 0?无限次,因为 0 没有价值,因此您从 15 中什么也得不到。您是否难以接受 15 - 0 = 15 而不是 ERROR?因此,如果我们将此类比切换回 Java 编码,则字符串 "" 表示没有值。随机选取一个字符串,说“hello world”,“hello world”可以减去多少次?

回答by Jan

Thinking of a string as a set of characters, in mathematics the empty set is always a subset of any set.

将字符串视为一组字符,在数学中,空集始终是任何集的子集。