scolebourne    2003/03/23 16:47:03

  Modified:    lang/src/test/org/apache/commons/lang
                        StringUtilsTrimEmptyTest.java
               lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Add trimToNull
  Add trimToEmpty
  
  Revision  Changes    Path
  1.7       +25 -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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- StringUtilsTrimEmptyTest.java     23 Mar 2003 21:51:19 -0000      1.6
  +++ StringUtilsTrimEmptyTest.java     24 Mar 2003 00:47:02 -0000      1.7
  @@ -97,6 +97,8 @@
           assertEquals(FOO, StringUtils.clean(" " + FOO + "  "));
           assertEquals(FOO, StringUtils.clean(" " + FOO));
           assertEquals(FOO, StringUtils.clean(FOO + ""));
  +        assertEquals("", StringUtils.clean(" \t\r\n\b "));
  +        assertEquals("", StringUtils.clean(""));
           assertEquals("", StringUtils.clean(null));
       }
   
  @@ -105,7 +107,29 @@
           assertEquals(FOO, StringUtils.trim(" " + FOO + "  "));
           assertEquals(FOO, StringUtils.trim(" " + FOO));
           assertEquals(FOO, StringUtils.trim(FOO + ""));
  +        assertEquals("", StringUtils.trim(" \t\r\n\b "));
  +        assertEquals("", StringUtils.trim(""));
           assertEquals(null, StringUtils.trim(null));
  +    }
  +
  +    public void testTrimToNull() {
  +        assertEquals(FOO, StringUtils.trimToNull(FOO + "  "));
  +        assertEquals(FOO, StringUtils.trimToNull(" " + FOO + "  "));
  +        assertEquals(FOO, StringUtils.trimToNull(" " + FOO));
  +        assertEquals(FOO, StringUtils.trimToNull(FOO + ""));
  +        assertEquals(null, StringUtils.trimToNull(" \t\r\n\b "));
  +        assertEquals(null, StringUtils.trimToNull(""));
  +        assertEquals(null, StringUtils.trimToNull(null));
  +    }
  +
  +    public void testTrimToEmpty() {
  +        assertEquals(FOO, StringUtils.trimToEmpty(FOO + "  "));
  +        assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO + "  "));
  +        assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO));
  +        assertEquals(FOO, StringUtils.trimToEmpty(FOO + ""));
  +        assertEquals("", StringUtils.trimToEmpty(" \t\r\n\b "));
  +        assertEquals("", StringUtils.trimToEmpty(""));
  +        assertEquals("", StringUtils.trimToEmpty(null));
       }
   
       public void testIsNotEmpty() {
  
  
  
  1.38      +76 -10    
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.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- StringUtils.java  23 Mar 2003 21:52:31 -0000      1.37
  +++ StringUtils.java  24 Mar 2003 00:47:02 -0000      1.38
  @@ -76,6 +76,7 @@
    * @author Holger Krauth
    * @author <a href="mailto:[EMAIL PROTECTED]">Alexander Day Chaffee</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
  + * @author Arun Mammen Thomas
    * @since 1.0
    * @version $Id$
    */
  @@ -99,6 +100,14 @@
        * <p>Removes control characters, including whitespace, from both
        * ends of this String, handling <code>null</code> by returning
        * an empty String.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.clean("abc")         = "abc"
  +     * StringUtils.clean("    abc    ") = "abc"
  +     * StringUtils.clean("     ")       = ""
  +     * StringUtils.clean("")            = ""
  +     * StringUtils.clean(null)          = ""
  +     * </pre>
        *
        * @see java.lang.String#trim()
        * @param str the String to check
  @@ -112,15 +121,72 @@
        * <p>Removes control characters, including whitespace, from both
        * ends of this String, handling <code>null</code> by returning
        * <code>null</code>.</p>
  +     * 
  +     * <p>The string is trimmed using [EMAIL PROTECTED] String#trim()}.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.trim("abc")         = "abc"
  +     * StringUtils.trim("    abc    ") = "abc"
  +     * StringUtils.trim("     ")       = ""
  +     * StringUtils.trim("")            = ""
  +     * StringUtils.trim(null)          = null
  +     * </pre>
        *
        * @see java.lang.String#trim()
  -     * @param str the String to check
  +     * @param str the String to be trimmed
        * @return the trimmed text (or <code>null</code>)
        */
       public static String trim(String str) {
           return (str == null ? null : str.trim());
       }
   
  +    /** 
  +     * <p>Removes control characters, including whitespace, from both  
  +     * ends of this string returning <code>null</code> if the string is 
  +     * empty after the trim or if it is <code>null</code>.
  +     * 
  +     * <p>The string is trimmed using [EMAIL PROTECTED] String#trim()}.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.trimToNull("abc")         = "abc"
  +     * StringUtils.trimToNull("    abc    ") = "abc"
  +     * StringUtils.trimToNull("     ")       = null
  +     * StringUtils.trimToNull("")            = null
  +     * StringUtils.trimToNull(null)          = null
  +     * </pre>
  +     *  
  +     * @see java.lang.String#trim()
  +     * @param str the String to be trimmed.
  +     * @return the trimmed string, or null if it's empty or null
  +     */
  +    public static String trimToNull(String str) {
  +        String ts = trim(str);
  +        return (ts == null || ts.length() == 0 ? null : ts);
  +    }
  +
  +    /** 
  +     * <p>Removes control characters, including whitespace, from both 
  +     * ends of this string returning an empty string if the string is
  +     * empty after the trim or if it is <code>null</code>.
  +     * 
  +     * <p>The string is trimmed using [EMAIL PROTECTED] String#trim()}.</p>
  +     * 
  +     * <pre>
  +     * StringUtils.trimToEmpty("abc")         = "abc"
  +     * StringUtils.trimToEmpty("    abc    ") = "abc"
  +     * StringUtils.trimToEmpty("     ")       = ""
  +     * StringUtils.trimToEmpty("")            = ""
  +     * StringUtils.trimToEmpty(null)          = ""
  +     * </pre>
  +     *  
  +     * @see java.lang.String#trim()
  +     * @param str the String to be trimmed
  +     * @return the trimmed string, or an empty string if it's empty or null
  +     */
  +    public static String trimToEmpty(String str) {
  +        return (str == null ? "" : str.trim());
  +    }
  +
       /**
        * <p>Deletes all 'space' characters from a String.</p>
        *
  @@ -1708,8 +1774,8 @@
       /**
        * <p>Checks if the String contains only certain chars.</p>
        *
  -     * @param str  the string to check
  -     * @param validChars  a string of valid chars
  +     * @param str the String to check
  +     * @param validChars a string of valid chars
        * @return true if it only contains valid chars and is non-null
        */
       public static boolean containsOnly(String str, String validChars) {
  @@ -1722,8 +1788,8 @@
       /**
        * <p>Checks if the String contains only certain chars.</p>
        *
  -     * @param str  the string to check
  -     * @param validChars  an array of valid chars
  +     * @param str the String to check
  +     * @param validChars an array of valid chars
        * @return true if it only contains valid chars and is non-null
        */
       public static boolean containsOnly(String str, char[] validChars) {
  @@ -1751,8 +1817,8 @@
       /**
        * <p>Checks that the String does not contain certain chars.</p>
        *
  -     * @param str  the string to check
  -     * @param invalidChars  a string of invalid chars
  +     * @param str the String to check
  +     * @param invalidChars a string of invalid chars
        * @return true if it contains none of the invalid chars, or is null
        */
       public static boolean containsNone(String str, String invalidChars) {
  @@ -1765,8 +1831,8 @@
       /**
        * <p>Checks that the String does not contain certain chars.</p>
        *
  -     * @param str  the string to check
  -     * @param invalidChars  an array of invalid chars
  +     * @param str the String to check
  +     * @param invalidChars an array of invalid chars
        * @return true if it contains none of the invalid chars, or is null
        */
       public static boolean containsNone(String str, char[] invalidChars) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to