【问题标题】:String.contains in JavaJava 中的 String.contains
【发布时间】: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返回t​​rue。 既然 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 

标签: java string


【解决方案1】:

空是任何字符串的子集。

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

在任何大小的线上都有无限数量的点......

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

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

【讨论】:

  • 并不是无限的 - 可能是字符数 + 1,因为每个字符之前都有一个空字符串,最后是另一个。
  • 你不认为“”和一个字母之间可能有一个“”吗? :)
  • 再次阅读您的答案,并意识到您所说的“线”是在一张纸上画的线。每个“”之间实际上可能有一个元“”;-)
【解决方案2】:

同样:

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

因此,任何String 中似乎都包含一个空字符串。

【讨论】:

    【解决方案3】:

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

    调用堆栈:

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

    【讨论】:

      【解决方案4】:

      我会用数学类比回答你的问题:

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

      【讨论】:

        【解决方案5】:

        对此的明显答案是“JLS 就是这么说的。”

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

        所以你有这样的东西:

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

        其中一些 s 是空字符串。

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 2011-05-04
            • 1970-01-01
            • 2021-05-11
            • 1970-01-01
            • 2014-01-30
            • 2016-05-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多