Add remove() methods to StringUtils

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137895 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-08-15 23:47:05 +00:00
parent 48e8e7e2e6
commit 98a80ec615
2 changed files with 112 additions and 3 deletions

View File

@ -111,7 +111,7 @@ import java.util.List;
* @author Al Chou
* @author Michael Davey
* @since 1.0
* @version $Id: StringUtils.java,v 1.132 2004/07/30 01:55:42 ggregory Exp $
* @version $Id: StringUtils.java,v 1.133 2004/08/15 23:47:05 scolebourne Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -2691,6 +2691,69 @@ public class StringUtils {
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
//-----------------------------------------------------------------------
/**

View File

@ -38,7 +38,7 @@ import junit.textui.TestRunner;
* @author Phil Steitz
* @author Gary D. Gregory
* @author Al Chou
* @version $Id: StringUtilsTest.java,v 1.60 2004/07/11 16:49:07 stevencaswell Exp $
* @version $Id: StringUtilsTest.java,v 1.61 2004/08/15 23:47:05 scolebourne Exp $
*/
public class StringUtilsTest extends TestCase {
@ -1322,5 +1322,51 @@ public class StringUtilsTest extends TestCase {
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'));
}
}