scolebourne    2004/08/15 16:47:05

  Modified:    lang/src/test/org/apache/commons/lang StringUtilsTest.java
               lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Add remove() methods to StringUtils
  
  Revision  Changes    Path
  1.61      +48 -2     
jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java
  
  Index: StringUtilsTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v
  retrieving revision 1.60
  retrieving revision 1.61
  diff -u -r1.60 -r1.61
  --- StringUtilsTest.java      11 Jul 2004 16:49:07 -0000      1.60
  +++ StringUtilsTest.java      15 Aug 2004 23:47:05 -0000      1.61
  @@ -1322,5 +1322,51 @@
           assertEquals(StringUtils.removeEnd("domain.com", ""), "domain.com");   
           assertEquals(StringUtils.removeEnd("domain.com", null), "domain.com");   
       }
  -}
   
  +    public void testRemove_String() {
  +        // StringUtils.remove(null, *)        = null
  +        assertEquals(null, StringUtils.remove(null, null));
  +        assertEquals(null, StringUtils.remove(null, ""));
  +        assertEquals(null, StringUtils.remove(null, "a"));
  +        
  +        // StringUtils.remove("", *)          = ""
  +        assertEquals("", StringUtils.remove("", null));
  +        assertEquals("", StringUtils.remove("", ""));
  +        assertEquals("", StringUtils.remove("", "a"));
  +        
  +        // StringUtils.remove(*, null)        = *
  +        assertEquals(null, StringUtils.remove(null, null));
  +        assertEquals("", StringUtils.remove("", null));
  +        assertEquals("a", StringUtils.remove("a", null));
  +        
  +        // StringUtils.remove(*, "")          = *
  +        assertEquals(null, StringUtils.remove(null, ""));
  +        assertEquals("", StringUtils.remove("", ""));
  +        assertEquals("a", StringUtils.remove("a", ""));
  +        
  +        // StringUtils.remove("queued", "ue") = "qd"
  +        assertEquals("qd", StringUtils.remove("queued", "ue"));
  +        
  +        // StringUtils.remove("queued", "zz") = "queued"
  +        assertEquals("queued", StringUtils.remove("queued", "zz"));
  +    }
  +
  +    public void testRemove_char() {
  +        // StringUtils.remove(null, *)       = null
  +        assertEquals(null, StringUtils.remove(null, 'a'));
  +        assertEquals(null, StringUtils.remove(null, 'a'));
  +        assertEquals(null, StringUtils.remove(null, 'a'));
  +        
  +        // StringUtils.remove("", *)          = ""
  +        assertEquals("", StringUtils.remove("", 'a'));
  +        assertEquals("", StringUtils.remove("", 'a'));
  +        assertEquals("", StringUtils.remove("", 'a'));
  +        
  +        // StringUtils.remove("queued", 'u') = "qeed"
  +        assertEquals("qeed", StringUtils.remove("queued", 'u'));
  +        
  +        // StringUtils.remove("queued", 'z') = "queued"
  +        assertEquals("queued", StringUtils.remove("queued", 'z'));
  +    }
  +
  +}
  
  
  
  1.133     +64 -1     
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.132
  retrieving revision 1.133
  diff -u -r1.132 -r1.133
  --- StringUtils.java  30 Jul 2004 01:55:42 -0000      1.132
  +++ StringUtils.java  15 Aug 2004 23:47:05 -0000      1.133
  @@ -2691,6 +2691,69 @@
           return str;
       }
   
  +    /**
  +     * <p>Removes all occurances of a substring from within the source string.</p>
  +     *
  +     * <p>A <code>null</code> source string will return <code>null</code>.
  +     * An empty ("") source string will return the empty string.
  +     * A <code>null</code> remove string will return the source string.
  +     * An empty ("") remove string will return the source string.</p>
  +     *
  +     * <pre>
  +     * StringUtils.remove(null, *)        = null
  +     * StringUtils.remove("", *)          = ""
  +     * StringUtils.remove(*, null)        = *
  +     * StringUtils.remove(*, "")          = *
  +     * StringUtils.remove("queued", "ue") = "qd"
  +     * StringUtils.remove("queued", "zz") = "queued"
  +     * </pre>
  +     *
  +     * @param str  the source String to search, may be null
  +     * @param remove  the String to search for and remove, may be null
  +     * @return the substring with the string removed if found,
  +     *  <code>null</code> if null String input
  +     * @since 2.1
  +     */
  +    public static String remove(String str, String remove) {
  +        if (isEmpty(str) || isEmpty(remove)) {
  +            return str;
  +        }
  +        return replace(str, remove, "", -1);
  +    }
  +
  +    /**
  +     * <p>Removes all occurances of a character from within the source string.</p>
  +     *
  +     * <p>A <code>null</code> source string will return <code>null</code>.
  +     * An empty ("") source string will return the empty string.</p>
  +     *
  +     * <pre>
  +     * StringUtils.remove(null, *)       = null
  +     * StringUtils.remove("", *)         = ""
  +     * StringUtils.remove("queued", 'u') = "qeed"
  +     * StringUtils.remove("queued", 'z') = "queued"
  +     * </pre>
  +     *
  +     * @param str  the source String to search, may be null
  +     * @param remove  the char to search for and remove, may be null
  +     * @return the substring with the char removed if found,
  +     *  <code>null</code> if null String input
  +     * @since 2.1
  +     */
  +    public static String remove(String str, char remove) {
  +        if (isEmpty(str) || str.indexOf(remove) == -1) {
  +            return str;
  +        }
  +        char[] chars = str.toCharArray();
  +        int pos = 0;
  +        for (int i = 0; i < chars.length; i++) {
  +            if (chars[i] != remove) {
  +                chars[pos++] = chars[i];
  +            }
  +        }
  +        return new String(chars, 0, pos);
  +    }
  +
       // Replacing
       //-----------------------------------------------------------------------
       /**
  
  
  

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

Reply via email to