commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r697715 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/StringUtils.java test/org/apache/commons/lang/StringUtilsTest.java
Date Mon, 22 Sep 2008 08:53:07 GMT
Author: sebb
Date: Mon Sep 22 01:53:07 2008
New Revision: 697715

URL: http://svn.apache.org/viewvc?rev=697715&view=rev
Log:
LANG-445 - new method StringUtils.startsWithAny(String str, String[] searchStrs)

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.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=697715&r1=697714&r2=697715&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 Mon Sep 22
01:53:07 2008
@@ -5943,6 +5943,38 @@
         }
         return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
     }
+    
+    /**
+     * <p>Check if a String starts with any of an array of specified strings.</p>
+     * 
+     * <pre>
+     * StringUtils.startsWithAny(null, null)      = false
+     * StringUtils.startsWithAny(null, new String[] {"abc"})  = false
+     * StringUtils.startsWithAny("abcxyz", null)     = false
+     * StringUtils.startsWithAny("abcxyz", new String[] {""}) = false
+     * StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true
+     * StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
+     * </pre>
+     *
+     * @see java.lang.String#startsWithAny(String, String[])
+     * @param string  the String to check, may be null
+     * @param searchStrings the Strings to find, may be null or empty
+     * @return <code>true</code> if the String starts with any of the the prefixes,
case insensitive, or
+     *  both <code>null</code>
+     * @since 3.0
+     */
+    public static boolean startsWithAny(String string, String[] searchStrings) {
+        if (isEmpty(string) || ArrayUtils.isEmpty(searchStrings)) {
+            return false;
+        }
+        for (int i = 0; i < searchStrings.length; i++) {
+            String searchString = searchStrings[i];
+            if (StringUtils.startsWith(string, searchString)) {
+                return true;
+            }
+        }
+        return false;
+    }
 
     // endsWith
     //-----------------------------------------------------------------------

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java?rev=697715&r1=697714&r2=697715&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java Mon Sep
22 01:53:07 2008
@@ -1837,5 +1837,15 @@
         assertEquals("", StringUtils.getCommonPrefix(new String[] {"xyz", "abcde"}));
         assertEquals("i am a ", StringUtils.getCommonPrefix(new String[] {"i am a machine",
"i am a robot"}));
     }
+        
+    public void testStartsWithAny() {
+        assertFalse(StringUtils.startsWithAny(null, null));
+        assertFalse(StringUtils.startsWithAny(null, new String[] {"abc"}));
+        assertFalse(StringUtils.startsWithAny("abcxyz", null));
+        assertFalse(StringUtils.startsWithAny("abcxyz", new String[] {}));
+        assertTrue(StringUtils.startsWithAny("abcxyz", new String[] {"abc"}));
+        assertTrue(StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}));
+        assertFalse(StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abcd"}));
+    }
  
 }



Mime
View raw message