From 95c2d8bbad7ac41ca6882c72bda7d0073663f01c Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Fri, 1 Aug 2003 22:05:43 +0000 Subject: [PATCH] Add substringBetween() as a replacement for getNestedString() git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137550 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/StringUtils.java | 188 ++++++++++++------ .../lang/StringUtilsSubstringTest.java | 23 ++- 2 files changed, 145 insertions(+), 66 deletions(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 497376fc3..889fa0677 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -144,7 +144,7 @@ import java.util.List; * @author Gary Gregory * @author Phil Steitz * @since 1.0 - * @version $Id: StringUtils.java,v 1.85 2003/08/01 21:02:16 scolebourne Exp $ + * @version $Id: StringUtils.java,v 1.86 2003/08/01 22:05:43 scolebourne Exp $ */ public class StringUtils { // Performance testing notes (JDK 1.4, Jul03, scolebourne) @@ -1691,6 +1691,128 @@ public class StringUtils { return str.substring(pos + separator.length()); } + // Substring between + //----------------------------------------------------------------------- + /** + *

Gets the String that is nested in between two instances of the + * same String.

+ * + *

A null input String returns null. + * A null tag returns null.

+ * + *
+     * StringUtils.substringBetween(null, *)            = null
+     * StringUtils.substringBetween("", "")             = ""
+     * StringUtils.substringBetween("", "tag")          = null
+     * StringUtils.substringBetween("tagabctag", null)  = null
+     * StringUtils.substringBetween("tagabctag", "")    = ""
+     * StringUtils.substringBetween("tagabctag", "tag") = "abc"
+     * 
+ * + * @param str the String containing the substring, may be null + * @param tag the String before and after the substring, may be null + * @return the substring, null if no match + */ + public static String substringBetween(String str, String tag) { + return substringBetween(str, tag, tag); + } + + /** + *

Gets the String that is nested in between two Strings. + * Only the first match is returned.

+ * + *

A null input String returns null. + * A null open/close returns null (no match). + * An empty ("") open/close returns an empty string.

+ * + *
+     * StringUtils.substringBetween(null, *, *)          = null
+     * StringUtils.substringBetween("", "", "")          = ""
+     * StringUtils.substringBetween("", "", "tag")       = null
+     * StringUtils.substringBetween("", "tag", "tag")    = null
+     * StringUtils.substringBetween("yabcz", null, null) = null
+     * StringUtils.substringBetween("yabcz", "", "")     = ""
+     * StringUtils.substringBetween("yabcz", "y", "z")   = "abc"
+     * StringUtils.substringBetween("yabczyabcz", "y", "z")   = "abc"
+     * 
+ * + * @param str the String containing the substring, may be null + * @param open the String before the substring, may be null + * @param close the String after the substring, may be null + * @return the substring, null if no match + */ + public static String substringBetween(String str, String open, String close) { + if (str == null || open == null || close == null) { + return null; + } + int start = str.indexOf(open); + if (start != -1) { + int end = str.indexOf(close, start + open.length()); + if (end != -1) { + return str.substring(start + open.length(), end); + } + } + return null; + } + + // Nested extraction + //----------------------------------------------------------------------- + /** + *

Gets the String that is nested in between two instances of the + * same String.

+ * + *

A null input String returns null. + * A null tag returns null.

+ * + *
+     * StringUtils.getNestedString(null, *)            = null
+     * StringUtils.getNestedString("", "")             = ""
+     * StringUtils.getNestedString("", "tag")          = null
+     * StringUtils.getNestedString("tagabctag", null)  = null
+     * StringUtils.getNestedString("tagabctag", "")    = ""
+     * StringUtils.getNestedString("tagabctag", "tag") = "abc"
+     * 
+ * + * @param str the String containing nested-string, may be null + * @param tag the String before and after nested-string, may be null + * @return the nested String, null if no match + * @deprecated Use the better named {@link #substringBetween(String, String)}. + * Method will be removed in Commons Lang 3.0. + */ + public static String getNestedString(String str, String tag) { + return substringBetween(str, tag, tag); + } + + /** + *

Gets the String that is nested in between two Strings. + * Only the first match is returned.

+ * + *

A null input String returns null. + * A null open/close returns null (no match). + * An empty ("") open/close returns an empty string.

+ * + *
+     * StringUtils.getNestedString(null, *, *)          = null
+     * StringUtils.getNestedString("", "", "")          = ""
+     * StringUtils.getNestedString("", "", "tag")       = null
+     * StringUtils.getNestedString("", "tag", "tag")    = null
+     * StringUtils.getNestedString("yabcz", null, null) = null
+     * StringUtils.getNestedString("yabcz", "", "")     = ""
+     * StringUtils.getNestedString("yabcz", "y", "z")   = "abc"
+     * StringUtils.getNestedString("yabczyabcz", "y", "z")   = "abc"
+     * 
+ * + * @param str the String containing nested-string, may be null + * @param open the String before nested-string, may be null + * @param close the String after nested-string, may be null + * @return the nested String, null if no match + * @deprecated Use the better named {@link #substringBetween(String, String, String)}. + * Method will be removed in Commons Lang 3.0. + */ + public static String getNestedString(String str, String open, String close) { + return substringBetween(str, open, close); + } + // Splitting //----------------------------------------------------------------------- /** @@ -3342,70 +3464,6 @@ public class StringUtils { return buffer.toString(); } - // Nested extraction - //----------------------------------------------------------------------- - /** - *

Gets the String that is nested in between two instances of the - * same String.

- * - *

A null input String returns null. - * A null tag returns null.

- * - *
-     * StringUtils.getNestedString(null, *)            = null
-     * StringUtils.getNestedString("", "")             = ""
-     * StringUtils.getNestedString("", "tag")          = null
-     * StringUtils.getNestedString("tagabctag", null)  = null
-     * StringUtils.getNestedString("tagabctag", "")    = ""
-     * StringUtils.getNestedString("tagabctag", "tag") = "abc"
-     * 
- * - * @param str the String containing nested-string, may be null - * @param tag the String before and after nested-string, may be null - * @return the nested String, null if no match - */ - public static String getNestedString(String str, String tag) { - return getNestedString(str, tag, tag); - } - - /** - *

Gets the String that is nested in between two Strings. - * Only the first match is returned.

- * - *

A null input String returns null. - * A null open/close returns null (no match). - * An empty ("") open/close returns an empty string.

- * - *
-     * StringUtils.getNestedString(null, *, *)          = null
-     * StringUtils.getNestedString("", "", "")          = ""
-     * StringUtils.getNestedString("", "", "tag")       = null
-     * StringUtils.getNestedString("", "tag", "tag")    = null
-     * StringUtils.getNestedString("yabcz", null, null) = null
-     * StringUtils.getNestedString("yabcz", "", "")     = ""
-     * StringUtils.getNestedString("yabcz", "y", "z")   = "abc"
-     * StringUtils.getNestedString("yabczyabcz", "y", "z")   = "abc"
-     * 
- * - * @param str the String containing nested-string, may be null - * @param open the String before nested-string, may be null - * @param close the String after nested-string, may be null - * @return the nested String, null if no match - */ - public static String getNestedString(String str, String open, String close) { - if (str == null || open == null || close == null) { - return null; - } - int start = str.indexOf(open); - if (start != -1) { - int end = str.indexOf(close, start + open.length()); - if (end != -1) { - return str.substring(start + open.length(), end); - } - } - return null; - } - // Count matches //----------------------------------------------------------------------- /** diff --git a/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java b/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java index 5309f4c50..0eac11137 100644 --- a/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java @@ -64,7 +64,7 @@ import junit.textui.TestRunner; * @author Stephen Colebourne * @author Ringo De Smet * @author Phil Steitz - * @version $Id: StringUtilsSubstringTest.java,v 1.10 2003/07/30 22:21:39 scolebourne Exp $ + * @version $Id: StringUtilsSubstringTest.java,v 1.11 2003/08/01 22:05:43 scolebourne Exp $ */ public class StringUtilsSubstringTest extends TestCase { private static final String FOO = "foo"; @@ -268,6 +268,27 @@ public class StringUtilsSubstringTest extends TestCase { assertEquals("", StringUtils.substringAfterLast("abc", "")); } + //----------------------------------------------------------------------- + public void testSubstringBetween_StringString() { + assertEquals(null, StringUtils.substringBetween(null, "tag")); + assertEquals("", StringUtils.substringBetween("", "")); + assertEquals(null, StringUtils.substringBetween("", "abc")); + assertEquals("", StringUtils.substringBetween(" ", " ")); + assertEquals(null, StringUtils.substringBetween("abc", null)); + assertEquals("", StringUtils.substringBetween("abc", "")); + assertEquals(null, StringUtils.substringBetween("abc", "a")); + assertEquals("bc", StringUtils.substringBetween("abca", "a")); + assertEquals("bc", StringUtils.substringBetween("abcabca", "a")); + assertEquals("bar", StringUtils.substringBetween("\nbar\n", "\n")); + } + + public void testSubstringBetween_StringStringString() { + assertEquals(null, StringUtils.substringBetween(null, "", "")); + assertEquals("", StringUtils.substringBetween("", "", "")); + assertEquals("", StringUtils.substringBetween(" ", " ", " ")); + assertEquals("bar", StringUtils.substringBetween("bar", "", "") ); + } + //----------------------------------------------------------------------- public void testCountMatches_String() { assertEquals(0, StringUtils.countMatches(null, null));