commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang StringUtils.java
Date Fri, 01 Aug 2003 22:05:43 GMT
scolebourne    2003/08/01 15:05:43

  Modified:    lang/src/test/org/apache/commons/lang
                        StringUtilsSubstringTest.java
               lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Add  substringBetween()  as a replacement for getNestedString()
  
  Revision  Changes    Path
  1.11      +22 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java
  
  Index: StringUtilsSubstringTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- StringUtilsSubstringTest.java	30 Jul 2003 22:21:39 -0000	1.10
  +++ StringUtilsSubstringTest.java	1 Aug 2003 22:05:43 -0000	1.11
  @@ -269,6 +269,27 @@
       }        
           
       //-----------------------------------------------------------------------
  +    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("<foo>bar</foo>",
"<foo>", "</foo>") );
  +    }
  +
  +    //-----------------------------------------------------------------------
       public void testCountMatches_String() {
           assertEquals(0, StringUtils.countMatches(null, null));
           assertEquals(0, StringUtils.countMatches("blah", null));
  
  
  
  1.86      +123 -65   jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.85
  retrieving revision 1.86
  diff -u -r1.85 -r1.86
  --- StringUtils.java	1 Aug 2003 21:02:16 -0000	1.85
  +++ StringUtils.java	1 Aug 2003 22:05:43 -0000	1.86
  @@ -1691,6 +1691,128 @@
           return str.substring(pos + separator.length());
       }
   
  +    // Substring between
  +    //-----------------------------------------------------------------------
  +    /**
  +     * <p>Gets the String that is nested in between two instances of the
  +     * same String.</p>
  +     *
  +     * <p>A <code>null</code> input String returns <code>null</code>.
  +     * A <code>null</code> tag returns <code>null</code>.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.substringBetween(null, *)            = null
  +     * StringUtils.substringBetween("", "")             = ""
  +     * StringUtils.substringBetween("", "tag")          = null
  +     * StringUtils.substringBetween("tagabctag", null)  = null
  +     * StringUtils.substringBetween("tagabctag", "")    = ""
  +     * StringUtils.substringBetween("tagabctag", "tag") = "abc"
  +     * </pre>
  +     *
  +     * @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, <code>null</code> if no match
  +     */
  +    public static String substringBetween(String str, String tag) {
  +        return substringBetween(str, tag, tag);
  +    }
  +    
  +    /**
  +     * <p>Gets the String that is nested in between two Strings.
  +     * Only the first match is returned.</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 an empty string.</p>
  +     *
  +     * <pre>
  +     * 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"
  +     * </pre>
  +     *
  +     * @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, <code>null</code> 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
  +    //-----------------------------------------------------------------------
  +    /**
  +     * <p>Gets the String that is nested in between two instances of the
  +     * same String.</p>
  +     *
  +     * <p>A <code>null</code> input String returns <code>null</code>.
  +     * A <code>null</code> tag returns <code>null</code>.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.getNestedString(null, *)            = null
  +     * StringUtils.getNestedString("", "")             = ""
  +     * StringUtils.getNestedString("", "tag")          = null
  +     * StringUtils.getNestedString("tagabctag", null)  = null
  +     * StringUtils.getNestedString("tagabctag", "")    = ""
  +     * StringUtils.getNestedString("tagabctag", "tag") = "abc"
  +     * </pre>
  +     *
  +     * @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, <code>null</code> 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);
  +    }
  +    
  +    /**
  +     * <p>Gets the String that is nested in between two Strings.
  +     * Only the first match is returned.</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 an empty string.</p>
  +     *
  +     * <pre>
  +     * 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"
  +     * </pre>
  +     *
  +     * @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, <code>null</code> 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
       //-----------------------------------------------------------------------
       /**
  @@ -3340,70 +3462,6 @@
               }
           }
           return buffer.toString();
  -    }
  -
  -    // Nested extraction
  -    //-----------------------------------------------------------------------
  -    /**
  -     * <p>Gets the String that is nested in between two instances of the
  -     * same String.</p>
  -     *
  -     * <p>A <code>null</code> input String returns <code>null</code>.
  -     * A <code>null</code> tag returns <code>null</code>.</p>
  -     * 
  -     * <pre>
  -     * StringUtils.getNestedString(null, *)            = null
  -     * StringUtils.getNestedString("", "")             = ""
  -     * StringUtils.getNestedString("", "tag")          = null
  -     * StringUtils.getNestedString("tagabctag", null)  = null
  -     * StringUtils.getNestedString("tagabctag", "")    = ""
  -     * StringUtils.getNestedString("tagabctag", "tag") = "abc"
  -     * </pre>
  -     *
  -     * @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, <code>null</code> if no match
  -     */
  -    public static String getNestedString(String str, String tag) {
  -        return getNestedString(str, tag, tag);
  -    }
  -    
  -    /**
  -     * <p>Gets the String that is nested in between two Strings.
  -     * Only the first match is returned.</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 an empty string.</p>
  -     *
  -     * <pre>
  -     * 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"
  -     * </pre>
  -     *
  -     * @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, <code>null</code> 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
  
  
  

Mime
View raw message