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 Sun, 20 Jul 2003 00:04:13 GMT
scolebourne    2003/07/19 17:04:12

  Modified:    lang/src/test/org/apache/commons/lang
                        StringUtilsTrimEmptyTest.java
               lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Add new methods stripToNull/stripToEmpty to provide alternative to trim versions
  
  Revision  Changes    Path
  1.13      +21 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java
  
  Index: StringUtilsTrimEmptyTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- StringUtilsTrimEmptyTest.java	19 Jul 2003 21:55:05 -0000	1.12
  +++ StringUtilsTrimEmptyTest.java	20 Jul 2003 00:04:12 -0000	1.13
  @@ -199,6 +199,26 @@
               StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE
+ StringUtilsTest.WHITESPACE));
       }
       
  +    public void testStripToNull_String() {
  +        assertEquals(null, StringUtils.stripToNull(null));
  +        assertEquals(null, StringUtils.stripToNull(""));
  +        assertEquals(null, StringUtils.stripToNull("        "));
  +        assertEquals(null, StringUtils.stripToNull(StringUtilsTest.WHITESPACE));
  +        assertEquals("ab c", StringUtils.stripToNull("  ab c  "));
  +        assertEquals(StringUtilsTest.NON_WHITESPACE, 
  +            StringUtils.stripToNull(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE
+ StringUtilsTest.WHITESPACE));
  +    }
  +    
  +    public void testStripToEmpty_String() {
  +        assertEquals("", StringUtils.stripToEmpty(null));
  +        assertEquals("", StringUtils.stripToEmpty(""));
  +        assertEquals("", StringUtils.stripToEmpty("        "));
  +        assertEquals("", StringUtils.stripToEmpty(StringUtilsTest.WHITESPACE));
  +        assertEquals("ab c", StringUtils.stripToEmpty("  ab c  "));
  +        assertEquals(StringUtilsTest.NON_WHITESPACE, 
  +            StringUtils.stripToEmpty(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE
+ StringUtilsTest.WHITESPACE));
  +    }
  +    
       public void testStrip_StringString() {
           // null strip
           assertEquals(null, StringUtils.strip(null, null));
  
  
  
  1.68      +70 -9     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.67
  retrieving revision 1.68
  diff -u -r1.67 -r1.68
  --- StringUtils.java	19 Jul 2003 23:29:06 -0000	1.67
  +++ StringUtils.java	20 Jul 2003 00:04:12 -0000	1.68
  @@ -179,7 +179,8 @@
        * <code>null</code>.</p>
        * 
        * <p>The String is trimmed using {@link String#trim()}.
  -     * Trim removes start and end characters &lt;= 32.</p>
  +     * Trim removes start and end characters &lt;= 32.
  +     * To strip whitespace use {@link #strip(String)}.</p>
        * 
        * <p>To trim your choice of characters, use the
        * {@link #strip(String, String)} methods.</p>
  @@ -192,7 +193,6 @@
        * StringUtils.trim("")            = ""
        * </pre>
        *
  -     * @see java.lang.String#trim()
        * @param str  the String to be trimmed, may be null
        * @return the trimmed string, <code>null</code> if null String input
        */
  @@ -206,7 +206,8 @@
        * empty ("") after the trim or if it is <code>null</code>.
        * 
        * <p>The String is trimmed using {@link String#trim()}.
  -     * Trim removes start and end characters &lt;= 32.</p>
  +     * Trim removes start and end characters &lt;= 32.
  +     * To strip whitespace use {@link #stripToNull(String)}.</p>
        * 
        * <pre>
        * StringUtils.trimToNull(null)          = null
  @@ -216,7 +217,6 @@
        * StringUtils.trimToNull("")            = null
        * </pre>
        *  
  -     * @see java.lang.String#trim()
        * @param str  the String to be trimmed, may be null
        * @return the trimmed String, 
        *  <code>null</code> if only chars &lt;= 32, empty or null String
input
  @@ -232,7 +232,8 @@
        * is empty ("") after the trim or if it is <code>null</code>.
        * 
        * <p>The String is trimmed using {@link String#trim()}.
  -     * Trim removes start and end characters &lt;= 32.</p>
  +     * Trim removes start and end characters &lt;= 32.
  +     * To strip whitespace use {@link #stripToEmpty(String)}.</p>
        * 
        * <pre>
        * StringUtils.trimToEmpty(null)          = ""
  @@ -242,7 +243,6 @@
        * StringUtils.trimToEmpty("")            = ""
        * </pre>
        *  
  -     * @see java.lang.String#trim()
        * @param str  the String to be trimmed, may be null
        * @return the trimmed String, or an empty String if <code>null</code>
input
        */
  @@ -2510,14 +2510,17 @@
       //-----------------------------------------------------------------------
       
       /**
  -     * <p>Strips whitespace from the start and end of a String.
  -     * This is similar to {@link String#trim()} but instead removes whitespace.
  +     * <p>Strips whitespace from the start and end of a String.</p>
  +     * 
  +     * <p>This is similar to {@link String#trim()} but instead removes whitespace.
        * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
        * 
        * <p>A <code>null</code> input String returns <code>null</code>.</p>
        * 
        * <pre>
        * StringUtils.strip(null)     = null
  +     * StringUtils.strip("")       = ""
  +     * StringUtils.strip("   ")    = ""
        * StringUtils.strip("abc")    = "abc"
        * StringUtils.strip("  abc")  = "abc"
        * StringUtils.strip("abc  ")  = "abc"
  @@ -2532,6 +2535,61 @@
           return strip(str, null);
       }
       
  +    /** 
  +     * <p>Strips whitespace from the start and end of a String  returning
  +     * <code>null</code> if the String is empty ("") after the strip.</p>
  +     * 
  +     * <p>This is similar to {@link #trimToNull()} but instead removes whitespace.
  +     * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.strip(null)     = null
  +     * StringUtils.strip("")       = null
  +     * StringUtils.strip("   ")    = null
  +     * StringUtils.strip("abc")    = "abc"
  +     * StringUtils.strip("  abc")  = "abc"
  +     * StringUtils.strip("abc  ")  = "abc"
  +     * StringUtils.strip(" abc ")  = "abc"
  +     * StringUtils.strip(" ab c ") = "ab c"
  +     * </pre>
  +     *  
  +     * @param str  the String to be stripped, may be null
  +     * @return the stripped String, 
  +     *  <code>null</code> if whitespace, empty or null String input
  +     */
  +    public static String stripToNull(String str) {
  +        if (str == null) {
  +            return null;
  +        }
  +        str = strip(str, null);
  +        return (str.length() == 0 ? null : str);
  +    }
  +
  +    /** 
  +     * <p>Strips whitespace from the start and end of a String  returning
  +     * an empty String if <code>null</code> input.</p>
  +     * 
  +     * <p>This is similar to {@link #trimToEmpty()} but instead removes whitespace.
  +     * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.strip(null)     = ""
  +     * StringUtils.strip("")       = ""
  +     * StringUtils.strip("   ")    = ""
  +     * StringUtils.strip("abc")    = "abc"
  +     * StringUtils.strip("  abc")  = "abc"
  +     * StringUtils.strip("abc  ")  = "abc"
  +     * StringUtils.strip(" abc ")  = "abc"
  +     * StringUtils.strip(" ab c ") = "ab c"
  +     * </pre>
  +     *  
  +     * @param str  the String to be stripped, may be null
  +     * @return the trimmed String, or an empty String if <code>null</code>
input
  +     */
  +    public static String stripToEmpty(String str) {
  +        return (str == null ? "" : strip(str, null));
  +    }
  +    
       /**
        * <p>Strips any of a set of characters from the start and end of a String.
        * This is similar to {@link String#trim()} but allows the characters
  @@ -2557,6 +2615,9 @@
        * @return the stripped String, <code>null</code> if null String input
        */
       public static String strip(String str, String stripChars) {
  +        if (str == null || str.length() == 0) {
  +            return str;
  +        }
           str = stripStart(str, stripChars);
           return stripEnd(str, stripChars);
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message