Author: scolebourne Date: Wed Jan 3 17:20:30 2007 New Revision: 492377 URL: http://svn.apache.org/viewvc?view=rev&rev=492377 Log: LANG-275 - StringUtils substringsBetween, implemented by Dave Meikle
Modified: jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt jakarta/commons/proper/lang/trunk/project.xml jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java Modified: jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt?view=diff&rev=492377&r1=492376&r2=492377 ============================================================================== --- jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt (original) +++ jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt Wed Jan 3 17:20:30 2007 @@ -82,4 +82,5 @@ * [LANG-266] - Wish for StringUtils.join(Collection, *) * [LANG-310] - BooleanUtils isNotTrue/isNotFalse * [LANG-306] - StrBuilder appendln/appendAll/appendSeparator + * [LANG-275] - StringUtils substringsBetween Modified: jakarta/commons/proper/lang/trunk/project.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/project.xml?view=diff&rev=492377&r1=492376&r2=492377 ============================================================================== --- jakarta/commons/proper/lang/trunk/project.xml (original) +++ jakarta/commons/proper/lang/trunk/project.xml Wed Jan 3 17:20:30 2007 @@ -271,6 +271,9 @@ <name>Rand McNeely</name> </contributor> <contributor> + <name>Dave Meikle</name> + </contributor> + <contributor> <name>Nikolay Metchev</name> </contributor> <contributor> Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?view=diff&rev=492377&r1=492376&r2=492377 ============================================================================== --- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java (original) +++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java Wed Jan 3 17:20:30 2007 @@ -1859,14 +1859,16 @@ * * <p>A <code>null</code> input String returns <code>null</code>. * A <code>null</code> open/close returns <code>null</code> (no match). - * An empty ("") open/close returns an empty string.</p> + * An empty ("") open and close returns an empty string.</p> * * <pre> + * StringUtils.substringBetween("wx[b]yz", "[", "]") = "b" * StringUtils.substringBetween(null, *, *) = null + * StringUtils.substringBetween(*, null, *) = null + * StringUtils.substringBetween(*, *, null) = null * StringUtils.substringBetween("", "", "") = "" - * StringUtils.substringBetween("", "", "tag") = null - * StringUtils.substringBetween("", "tag", "tag") = null - * StringUtils.substringBetween("yabcz", null, null) = null + * StringUtils.substringBetween("", "", "]") = null + * StringUtils.substringBetween("", "[", "]") = null * StringUtils.substringBetween("yabcz", "", "") = "" * StringUtils.substringBetween("yabcz", "y", "z") = "abc" * StringUtils.substringBetween("yabczyabcz", "y", "z") = "abc" @@ -1890,6 +1892,60 @@ } } return null; + } + + /** + * <p>Searches a String for substrings delimited by a start and end tag, + * returning all matching substrings in an array.</p> + * + * <p>A <code>null</code> input String returns <code>null</code>. + * A <code>null</code> open/close returns <code>null</code> (no match). + * An empty ("") open/close returns <code>null</code> (no match).</p> + * + * <pre> + * StringUtils.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"] + * StringUtils.substringsBetween(null, *, *) = null + * StringUtils.substringsBetween(*, null, *) = null + * StringUtils.substringsBetween(*, *, null) = null + * StringUtils.substringsBetween("", "[", "]") = [] + * </pre> + * + * @param str the String containing the substrings, null returns null, empty returns empty + * @param open the String identifying the start of the substring, empty returns null + * @param close the String identifying the end of the substring, empty returns null + * @return a String Array of substrings, or <code>null</code> if no match + * @since 2.3 + */ + public static String[] substringsBetween(String str, String open, String close) { + if (str == null || isEmpty(open) || isEmpty(close)) { + return null; + } + int strLen = str.length(); + if (strLen == 0) { + return ArrayUtils.EMPTY_STRING_ARRAY; + } + int closeLen = close.length(); + int openLen = open.length(); + List list = new ArrayList(); + int pos = 0; + while (pos < (strLen - closeLen)) { + int start = str.indexOf(open, pos); + if (start < 0) { + break; + } + start += openLen; + int end = str.indexOf(close, start); + if (end < 0) { + break; + } + list.add(str.substring(start, end)); + pos = end + closeLen; + } + if (list.size() > 0) { + return (String[]) list.toArray(new String [list.size()]); + } else { + return null; + } } // Nested extraction Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java?view=diff&rev=492377&r1=492376&r2=492377 ============================================================================== --- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java (original) +++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java Wed Jan 3 17:20:30 2007 @@ -248,8 +248,71 @@ public void testSubstringBetween_StringStringString() { assertEquals(null, StringUtils.substringBetween(null, "", "")); assertEquals("", StringUtils.substringBetween("", "", "")); + assertEquals("", StringUtils.substringBetween("foo", "", "")); + assertEquals(null, StringUtils.substringBetween("foo", "", "]")); + assertEquals(null, StringUtils.substringBetween("foo", "[", "]")); assertEquals("", StringUtils.substringBetween(" ", " ", " ")); assertEquals("bar", StringUtils.substringBetween("<foo>bar</foo>", "<foo>", "</foo>") ); + } + + /** + * Tests the substringsBetween method that returns an String Array of substrings. + */ + public void testSubstringsBetween_StringStringString() { + + String[] results = StringUtils.substringsBetween("[one], [two], [three]", "[", "]"); + assertEquals(3, results.length); + assertEquals("one", results[0]); + assertEquals("two", results[1]); + assertEquals("three", results[2]); + + results = StringUtils.substringsBetween("[one], [two], three", "[", "]"); + assertEquals(2, results.length); + assertEquals("one", results[0]); + assertEquals("two", results[1]); + + results = StringUtils.substringsBetween("[one], [two], three]", "[", "]"); + assertEquals(2, results.length); + assertEquals("one", results[0]); + assertEquals("two", results[1]); + + results = StringUtils.substringsBetween("[one], two], three]", "[", "]"); + assertEquals(1, results.length); + assertEquals("one", results[0]); + + results = StringUtils.substringsBetween("one], two], [three]", "[", "]"); + assertEquals(1, results.length); + assertEquals("three", results[0]); + + // 'ab hello ba' will match, but 'ab non ba' won't + // this is because the 'a' is shared between the two and can't be matched twice + results = StringUtils.substringsBetween("aabhellobabnonba", "ab", "ba"); + assertEquals(1, results.length); + assertEquals("hello", results[0]); + + results = StringUtils.substringsBetween("one, two, three", "[", "]"); + assertNull(results); + + results = StringUtils.substringsBetween("[one, two, three", "[", "]"); + assertNull(results); + + results = StringUtils.substringsBetween("one, two, three]", "[", "]"); + assertNull(results); + + results = StringUtils.substringsBetween("[one], [two], [three]", "[", null); + assertNull(results); + + results = StringUtils.substringsBetween("[one], [two], [three]", null, "]"); + assertNull(results); + + results = StringUtils.substringsBetween("[one], [two], [three]", "", ""); + assertNull(results); + + results = StringUtils.substringsBetween(null, "[", "]"); + assertNull(results); + + results = StringUtils.substringsBetween("", "[", "]"); + assertEquals(0, results.length); } //----------------------------------------------------------------------- --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]