commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r595929 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/StringUtils.java test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java
Date Sat, 17 Nov 2007 09:53:13 GMT
Author: niallp
Date: Sat Nov 17 01:53:12 2007
New Revision: 595929

URL: http://svn.apache.org/viewvc?rev=595929&view=rev
Log:
LANG-377 Add containsAny() methods to StringUtils

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?rev=595929&r1=595928&r2=595929&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java Sat Nov 17
01:53:12 2007
@@ -40,8 +40,8 @@
  *      - null-safe index-of checks
  *  <li><b>IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut</b>
  *      - index-of any of a set of Strings</li>
- *  <li><b>ContainsOnly/ContainsNone</b>
- *      - does String contains only/none of these characters</li>
+ *  <li><b>ContainsOnly/ContainsNone/ContainsAny</b>
+ *      - does String contains only/none/any of these characters</li>
  *  <li><b>Substring/Left/Right/Mid</b>
  *      - null-safe substring extractions</li>
  *  <li><b>SubstringBefore/SubstringAfter/SubstringBetween</b>
@@ -1135,6 +1135,78 @@
             return -1;
         }
         return indexOfAny(str, searchChars.toCharArray());
+    }
+
+    // ContainsAny
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Checks if the String contains any character in the given
+     * set of characters.</p>
+     *
+     * <p>A <code>null</code> String will return <code>false</code>.
+     * A <code>null</code> or zero length search array will return <code>false</code>.</p>
+     *
+     * <pre>
+     * StringUtils.containsAny(null, *)                = false
+     * StringUtils.containsAny("", *)                  = false
+     * StringUtils.containsAny(*, null)                = false
+     * StringUtils.containsAny(*, [])                  = false
+     * StringUtils.containsAny("zzabyycdxx",['z','a']) = true
+     * StringUtils.containsAny("zzabyycdxx",['b','y']) = true
+     * StringUtils.containsAny("aba", ['z'])           = false
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @param searchChars  the chars to search for, may be null
+     * @return the <code>true</code> if any of the chars are found,
+     * <code>false</code> if no match or null input
+     * @since 2.4
+     */
+    public static boolean containsAny(String str, char[] searchChars) {
+        if (str == null || str.length() == 0 ||
+            searchChars == null || searchChars.length == 0) {
+            return false;
+        }
+        for (int i = 0; i < str.length(); i++) {
+            char ch = str.charAt(i);
+            for (int j = 0; j < searchChars.length; j++) {
+                if (searchChars[j] == ch) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
+     * <p>Checks if the String contains any character in the given
+     * set of characters.</p>
+     *
+     * <p>A <code>null</code> String will return <code>false</code>.
+     * A <code>null</code> search string will return <code>false</code>.</p>
+     *
+     * <pre>
+     * StringUtils.containsAny(null, *)            = false
+     * StringUtils.containsAny("", *)              = false
+     * StringUtils.containsAny(*, null)            = false
+     * StringUtils.containsAny(*, "")              = false
+     * StringUtils.containsAny("zzabyycdxx", "za") = true
+     * StringUtils.containsAny("zzabyycdxx", "by") = true
+     * StringUtils.containsAny("aba","z")          = false
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @param searchChars  the chars to search for, may be null
+     * @return the <code>true</code> if any of the chars are found,
+     * <code>false</code> if no match or null input
+     * @since 2.4
+     */
+    public static boolean containsAny(String str, String searchChars) {
+        if (searchChars == null) {
+            return false;
+        } else {
+            return containsAny(str, searchChars.toCharArray());
+        }
     }
 
     // IndexOfAnyBut chars

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java?rev=595929&r1=595928&r2=595929&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java
(original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java
Sat Nov 17 01:53:12 2007
@@ -378,6 +378,39 @@
     }
 
     //-----------------------------------------------------------------------
+    public void testContainsAny_StringChararray() {
+        assertFalse(StringUtils.containsAny(null, (char[]) null));
+        assertFalse(StringUtils.containsAny(null, new char[0]));
+        assertFalse(StringUtils.containsAny(null, new char[] {'a','b'}));
+        
+        assertFalse(StringUtils.containsAny("", (char[]) null));
+        assertFalse(StringUtils.containsAny("", new char[0]));
+        assertFalse(StringUtils.containsAny("", new char[] {'a','b'}));
+        
+        assertFalse(StringUtils.containsAny("zzabyycdxx", (char[]) null)); 
+        assertFalse(StringUtils.containsAny("zzabyycdxx", new char[0])); 
+        assertTrue(StringUtils.containsAny("zzabyycdxx", new char[] {'z','a'})); 
+        assertTrue(StringUtils.containsAny("zzabyycdxx", new char[] {'b','y'}));
+        assertFalse(StringUtils.containsAny("ab", new char[] {'z'}));
+    }
+
+    public void testContainsAny_StringString() {
+        assertFalse(StringUtils.containsAny(null, (String) null));
+        assertFalse(StringUtils.containsAny(null, ""));
+        assertFalse(StringUtils.containsAny(null, "ab"));
+        
+        assertFalse(StringUtils.containsAny("", (String) null));
+        assertFalse(StringUtils.containsAny("", ""));
+        assertFalse(StringUtils.containsAny("", "ab"));
+        
+        assertFalse(StringUtils.containsAny("zzabyycdxx", (String) null)); 
+        assertFalse(StringUtils.containsAny("zzabyycdxx", "")); 
+        assertTrue(StringUtils.containsAny("zzabyycdxx", "za")); 
+        assertTrue(StringUtils.containsAny("zzabyycdxx", "by"));
+        assertFalse(StringUtils.containsAny("ab", "z"));
+    }
+
+    //-----------------------------------------------------------------------
     public void testIndexOfAnyBut_StringChararray() {
         assertEquals(-1, StringUtils.indexOfAnyBut(null, (char[]) null));
         assertEquals(-1, StringUtils.indexOfAnyBut(null, new char[0]));



Mime
View raw message