Commons Lang
  1. Commons Lang
  2. LANG-263

[lang] Add StringUtils.containsIgnoreCase(...)

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2
    • Component/s: lang.*
    • Labels:
      None

      Description

      To complement StringUtils.equalsIgnoreCase(...), StringUtils.containsIgnoreCase(...) would be quite useful. A simple implementation would uppercase both strings and delegate to StringUtils.contains(...).

        Activity

        Hide
        Henri Yandell added a comment -

        Seems like a fair feature.

        Feel free to submit a patch + unit test and it'll make its way into 2.2

        Show
        Henri Yandell added a comment - Seems like a fair feature. Feel free to submit a patch + unit test and it'll make its way into 2.2
        Hide
        analogue added a comment -

        /**

        • <p>Checks if String contains a search String irrespective of case,
        • handling <code>null</code>. This method uses
        • {@link #contains(String, String)}

          .</p>
          *

        • <p>A <code>null</code> String will return <code>false</code>.</p>
          *
        • <pre>
        • StringUtils.contains(null, *) = false
        • StringUtils.contains(*, null) = false
        • StringUtils.contains("", "") = true
        • StringUtils.contains("abc", "") = true
        • StringUtils.contains("abc", "a") = true
        • StringUtils.contains("abc", "z") = false
        • StringUtils.contains("abc", "A") = true
        • StringUtils.contains("abc", "Z") = false
        • </pre>
          *
        • @param str the String to check, may be null
        • @param searchStr the String to find, may be null
        • @return true if the String contains the search String irrespective of
        • case or false if not or <code>null</code> string input
          */
          public static boolean containsIgnoreCase(String str, String searchStr)
          Unknown macro: { if (str == null || searchStr == null) { return false; } return contains(str.toUpperCase(), searchStr.toUpperCase()); }

        public void testContainsIgnoreCase_StringString()

        { assertFalse(StringUtils.containsIgnoreCase(null, null)); // Null tests assertFalse(StringUtils.containsIgnoreCase(null, "")); assertFalse(StringUtils.containsIgnoreCase(null, "a")); assertFalse(StringUtils.containsIgnoreCase(null, "abc")); assertFalse(StringUtils.containsIgnoreCase("", null)); assertFalse(StringUtils.containsIgnoreCase("a", null)); assertFalse(StringUtils.containsIgnoreCase("abc", null)); // Match len = 0 assertTrue(StringUtils.containsIgnoreCase("", "")); assertTrue(StringUtils.containsIgnoreCase("a", "")); assertTrue(StringUtils.containsIgnoreCase("abc", "")); // Match len = 1 assertFalse(StringUtils.containsIgnoreCase("", "a")); assertTrue(StringUtils.containsIgnoreCase("a", "a")); assertTrue(StringUtils.containsIgnoreCase("abc", "a")); assertFalse(StringUtils.containsIgnoreCase("", "A")); assertTrue(StringUtils.containsIgnoreCase("a", "A")); assertTrue(StringUtils.containsIgnoreCase("abc", "A")); // Match len > 1 assertFalse(StringUtils.containsIgnoreCase("", "abc")); assertFalse(StringUtils.containsIgnoreCase("a", "abc")); assertTrue(StringUtils.containsIgnoreCase("xabcz", "abc")); assertFalse(StringUtils.containsIgnoreCase("", "ABC")); assertFalse(StringUtils.containsIgnoreCase("a", "ABC")); assertTrue(StringUtils.containsIgnoreCase("xabcz", "ABC")); }
        Show
        analogue added a comment - /** <p>Checks if String contains a search String irrespective of case, handling <code>null</code>. This method uses {@link #contains(String, String)} .</p> * <p>A <code>null</code> String will return <code>false</code>.</p> * <pre> StringUtils.contains(null, *) = false StringUtils.contains(*, null) = false StringUtils.contains("", "") = true StringUtils.contains("abc", "") = true StringUtils.contains("abc", "a") = true StringUtils.contains("abc", "z") = false StringUtils.contains("abc", "A") = true StringUtils.contains("abc", "Z") = false </pre> * @param str the String to check, may be null @param searchStr the String to find, may be null @return true if the String contains the search String irrespective of case or false if not or <code>null</code> string input */ public static boolean containsIgnoreCase(String str, String searchStr) Unknown macro: { if (str == null || searchStr == null) { return false; } return contains(str.toUpperCase(), searchStr.toUpperCase()); } public void testContainsIgnoreCase_StringString() { assertFalse(StringUtils.containsIgnoreCase(null, null)); // Null tests assertFalse(StringUtils.containsIgnoreCase(null, "")); assertFalse(StringUtils.containsIgnoreCase(null, "a")); assertFalse(StringUtils.containsIgnoreCase(null, "abc")); assertFalse(StringUtils.containsIgnoreCase("", null)); assertFalse(StringUtils.containsIgnoreCase("a", null)); assertFalse(StringUtils.containsIgnoreCase("abc", null)); // Match len = 0 assertTrue(StringUtils.containsIgnoreCase("", "")); assertTrue(StringUtils.containsIgnoreCase("a", "")); assertTrue(StringUtils.containsIgnoreCase("abc", "")); // Match len = 1 assertFalse(StringUtils.containsIgnoreCase("", "a")); assertTrue(StringUtils.containsIgnoreCase("a", "a")); assertTrue(StringUtils.containsIgnoreCase("abc", "a")); assertFalse(StringUtils.containsIgnoreCase("", "A")); assertTrue(StringUtils.containsIgnoreCase("a", "A")); assertTrue(StringUtils.containsIgnoreCase("abc", "A")); // Match len > 1 assertFalse(StringUtils.containsIgnoreCase("", "abc")); assertFalse(StringUtils.containsIgnoreCase("a", "abc")); assertTrue(StringUtils.containsIgnoreCase("xabcz", "abc")); assertFalse(StringUtils.containsIgnoreCase("", "ABC")); assertFalse(StringUtils.containsIgnoreCase("a", "ABC")); assertTrue(StringUtils.containsIgnoreCase("xabcz", "ABC")); }
        Hide
        Henri Yandell added a comment -

        Thanks for the patch. Committed and will appear in Lang 2.2 (and nightly builds tonight).

        svn ci -m "Added containsIgnoreCase method - LANG-263" src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java src/java/org/apache/commons/lang/StringUtils.java
        Sending src/java/org/apache/commons/lang/StringUtils.java
        Sending src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java
        Transmitting file data ..
        Committed revision 412200.

        Show
        Henri Yandell added a comment - Thanks for the patch. Committed and will appear in Lang 2.2 (and nightly builds tonight). svn ci -m "Added containsIgnoreCase method - LANG-263 " src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java src/java/org/apache/commons/lang/StringUtils.java Sending src/java/org/apache/commons/lang/StringUtils.java Sending src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java Transmitting file data .. Committed revision 412200.

          People

          • Assignee:
            Unassigned
            Reporter:
            analogue
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development