Deprecate deleteSpaces()

Move delete methods next to replace


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137463 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-20 00:17:29 +00:00
parent 97adbab600
commit 30ddfad67f
3 changed files with 139 additions and 105 deletions

View File

@ -98,7 +98,7 @@ import org.apache.commons.lang.math.NumberUtils;
* @author Arun Mammen Thomas
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 1.0
* @version $Id: StringUtils.java,v 1.68 2003/07/20 00:04:12 scolebourne Exp $
* @version $Id: StringUtils.java,v 1.69 2003/07/20 00:17:29 scolebourne Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -250,66 +250,6 @@ public class StringUtils {
return (str == null ? "" : str.trim());
}
// Delete
//-----------------------------------------------------------------------
/**
* <p>Deletes all 'space' characters from a String as defined by
* {@link Character#isSpace(char)}.</p>
*
* <p>This is the only StringUtils method that uses the
* <code>isSpace</code> definition. You are advised to use
* {@link #deleteWhitespace(String)} instead as whitespace is much
* better localized.</p>
*
* <pre>
* StringUtils.deleteSpaces(null) = null
* StringUtils.deleteSpaces("abc") = "abc"
* StringUtils.deleteSpaces(" \t abc \n ") = "abc"
* StringUtils.deleteSpaces("ab c") = "abc"
* StringUtils.deleteSpaces("a\nb\tc ") = "abc"
* </pre>
*
* <p>Spaces are defined as <code>{' ', '\t', '\r', '\n', '\b'}</code>
* in line with the deprecated <code>isSpace</code> method.</p>
*
* @param str the String to delete spaces from, may be null
* @return the String without 'spaces', <code>null</code> if null String input
*/
public static String deleteSpaces(String str) {
if (str == null) {
return null;
}
return CharSetUtils.delete(str, " \t\r\n\b");
}
/**
* <p>Deletes all whitespaces from a String as defined by
* {@link Character#isWhitespace(char)}.</p>
*
* <pre>
* StringUtils.deleteWhitespace(null) = null
* StringUtils.deleteWhitespace("abc") = "abc"
* StringUtils.deleteWhitespace(" abc ") = "abc"
* </pre>
*
* @param str the String to delete whitespace from, may be null
* @return the String without whitespaces, <code>null</code> if null String input
*/
public static String deleteWhitespace(String str) {
if (str == null) {
return null;
}
int sz = str.length();
StringBuffer buffer = new StringBuffer(sz);
for (int i = 0; i < sz; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
buffer.append(str.charAt(i));
}
}
return buffer.toString();
}
// Empty checks
//-----------------------------------------------------------------------
@ -1577,11 +1517,73 @@ public class StringUtils {
return buf.toString();
}
// Delete
//-----------------------------------------------------------------------
/**
* <p>Deletes all 'space' characters from a String as defined by
* {@link Character#isSpace(char)}.</p>
*
* <p>This is the only StringUtils method that uses the
* <code>isSpace</code> definition. You are advised to use
* {@link #deleteWhitespace(String)} instead as whitespace is much
* better localized.</p>
*
* <pre>
* StringUtils.deleteSpaces(null) = null
* StringUtils.deleteSpaces("abc") = "abc"
* StringUtils.deleteSpaces(" \t abc \n ") = "abc"
* StringUtils.deleteSpaces("ab c") = "abc"
* StringUtils.deleteSpaces("a\nb\tc ") = "abc"
* </pre>
*
* <p>Spaces are defined as <code>{' ', '\t', '\r', '\n', '\b'}</code>
* in line with the deprecated <code>isSpace</code> method.</p>
*
* @param str the String to delete spaces from, may be null
* @return the String without 'spaces', <code>null</code> if null String input
* @deprecated Use the better localized {@link #deleteWhitespace(String)}.
* Method will be removed in Commons Lang 3.0.
*/
public static String deleteSpaces(String str) {
if (str == null) {
return null;
}
return CharSetUtils.delete(str, " \t\r\n\b");
}
/**
* <p>Deletes all whitespaces from a String as defined by
* {@link Character#isWhitespace(char)}.</p>
*
* <pre>
* StringUtils.deleteWhitespace(null) = null
* StringUtils.deleteWhitespace("abc") = "abc"
* StringUtils.deleteWhitespace(" abc ") = "abc"
* </pre>
*
* @param str the String to delete whitespace from, may be null
* @return the String without whitespaces, <code>null</code> if null String input
*/
public static String deleteWhitespace(String str) {
if (str == null) {
return null;
}
int sz = str.length();
StringBuffer buffer = new StringBuffer(sz);
for (int i = 0; i < sz; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
buffer.append(str.charAt(i));
}
}
return buffer.toString();
}
// Replacing
//-----------------------------------------------------------------------
/**
* <p>Replace a String with another String inside a larger String, once.</p>
* <p>Replaces a String with another String inside a larger String, once.</p>
*
* <p>A <code>null</code> reference passed to this method is a no-op.</p>
*
@ -1606,7 +1608,7 @@ public class StringUtils {
}
/**
* <p>Replace all occurances of a String within another String.</p>
* <p>Replaces all occurances of a String within another String.</p>
*
* <p>A <code>null</code> reference passed to this method is a no-op.</p>
*
@ -1631,7 +1633,7 @@ public class StringUtils {
}
/**
* <p>Replace a String with another String inside a larger String,
* <p>Replaces a String with another String inside a larger String,
* for the first <code>max</code> values of the search String.</p>
*
* <p>A <code>null</code> reference passed to this method is a no-op.</p>

View File

@ -72,7 +72,7 @@ import junit.textui.TestRunner;
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
* @author Holger Krauth
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @version $Id: StringUtilsTest.java,v 1.30 2003/07/19 23:28:23 scolebourne Exp $
* @version $Id: StringUtilsTest.java,v 1.31 2003/07/20 00:17:29 scolebourne Exp $
*/
public class StringUtilsTest extends TestCase {
@ -341,26 +341,78 @@ public class StringUtilsTest extends TestCase {
}
}
public void testReplaceFunctions() {
assertEquals("replace(String, String, String, int) failed",
FOO, StringUtils.replace("oo" + FOO, "o", "", 2));
assertEquals("replace(String, String, String) failed",
"", StringUtils.replace(FOO + FOO + FOO, FOO, ""));
assertEquals("replaceOnce(String, String, String) failed",
FOO, StringUtils.replaceOnce(FOO + FOO, FOO, ""));
assertEquals("carriage-return replace(String,String,String) failed",
"test123", StringUtils.replace("test\r1\r2\r3", "\r", ""));
public void testDeleteSpace_String() {
assertEquals(null, StringUtils.deleteSpaces(null));
assertEquals("", StringUtils.deleteSpaces(""));
assertEquals("", StringUtils.deleteSpaces(" \t\t\n\n "));
assertEquals("test", StringUtils.deleteSpaces("t \t\ne\rs\n\n \tt"));
}
public void testDeleteWhitespace_String() {
assertEquals(null, StringUtils.deleteWhitespace(null));
assertEquals("", StringUtils.deleteWhitespace(""));
assertEquals("", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B "));
assertEquals("", StringUtils.deleteWhitespace(StringUtilsTest.WHITESPACE));
assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.deleteWhitespace(StringUtilsTest.NON_WHITESPACE));
// Note: u-2007 and u-000A both cause problems in the source code
// it should ignore 2007 but delete 000A
assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F "));
assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F"));
assertEquals("test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt"));
}
assertEquals("replace(String, String, String) failed",
"FOO", StringUtils.replace("FOO", "", "any"));
assertEquals("replace(String, String, String) failed",
"FOO", StringUtils.replace("FOO", null, "any"));
assertEquals("replace(String, String, String) failed",
"FOO", StringUtils.replace("FOO", "F", null));
assertEquals("replace(String, String, String) failed",
"FOO", StringUtils.replace("FOO", null, null));
assertEquals("replace(String, String, String) failed",
null, StringUtils.replace(null, "", "any"));
public void testReplace_StringStringString() {
assertEquals(null, StringUtils.replace(null, null, null));
assertEquals(null, StringUtils.replace(null, null, "any"));
assertEquals(null, StringUtils.replace(null, "any", null));
assertEquals(null, StringUtils.replace(null, "any", "any"));
assertEquals("", StringUtils.replace("", null, null));
assertEquals("", StringUtils.replace("", null, "any"));
assertEquals("", StringUtils.replace("", "any", null));
assertEquals("", StringUtils.replace("", "any", "any"));
assertEquals("FOO", StringUtils.replace("FOO", "", "any"));
assertEquals("FOO", StringUtils.replace("FOO", null, "any"));
assertEquals("FOO", StringUtils.replace("FOO", "F", null));
assertEquals("FOO", StringUtils.replace("FOO", null, null));
assertEquals("", StringUtils.replace("foofoofoo", "foo", ""));
assertEquals("barbarbar", StringUtils.replace("foofoofoo", "foo", "bar"));
assertEquals("farfarfar", StringUtils.replace("foofoofoo", "oo", "ar"));
}
public void testReplace_StringStringStringInt() {
assertEquals(null, StringUtils.replace(null, null, null, 2));
assertEquals(null, StringUtils.replace(null, null, "any", 2));
assertEquals(null, StringUtils.replace(null, "any", null, 2));
assertEquals(null, StringUtils.replace(null, "any", "any", 2));
assertEquals("f", StringUtils.replace("oofoo", "o", "", -1));
assertEquals("oofoo", StringUtils.replace("oofoo", "o", "", 0));
assertEquals("ofoo", StringUtils.replace("oofoo", "o", "", 1));
assertEquals("foo", StringUtils.replace("oofoo", "o", "", 2));
assertEquals("fo", StringUtils.replace("oofoo", "o", "", 3));
assertEquals("f", StringUtils.replace("oofoo", "o", "", 4));
}
public void testReplaceOnce_StringStringString() {
assertEquals(null, StringUtils.replaceOnce(null, null, null));
assertEquals(null, StringUtils.replaceOnce(null, null, "any"));
assertEquals(null, StringUtils.replaceOnce(null, "any", null));
assertEquals(null, StringUtils.replaceOnce(null, "any", "any"));
assertEquals("", StringUtils.replaceOnce("", null, null));
assertEquals("", StringUtils.replaceOnce("", null, "any"));
assertEquals("", StringUtils.replaceOnce("", "any", null));
assertEquals("", StringUtils.replaceOnce("", "any", "any"));
assertEquals("FOO", StringUtils.replaceOnce("FOO", "", "any"));
assertEquals("FOO", StringUtils.replaceOnce("FOO", null, "any"));
assertEquals("FOO", StringUtils.replaceOnce("FOO", "F", null));
assertEquals("FOO", StringUtils.replaceOnce("FOO", null, null));
assertEquals("foofoo", StringUtils.replaceOnce("foofoofoo", "foo", ""));
}
public void testOverlayString() {

View File

@ -63,7 +63,7 @@ import junit.textui.TestRunner;
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: StringUtilsTrimEmptyTest.java,v 1.13 2003/07/20 00:04:12 scolebourne Exp $
* @version $Id: StringUtilsTrimEmptyTest.java,v 1.14 2003/07/20 00:17:29 scolebourne Exp $
*/
public class StringUtilsTrimEmptyTest extends TestCase {
private static final String FOO = "foo";
@ -170,26 +170,6 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(true, StringUtils.isNotBlank(" foo "));
}
public void testDeleteSpace() {
assertEquals(null, StringUtils.deleteSpaces(null));
assertEquals("", StringUtils.deleteSpaces(""));
assertEquals("", StringUtils.deleteSpaces(" \t\t\n\n "));
assertEquals("test", StringUtils.deleteSpaces("t \t\ne\rs\n\n \tt"));
}
public void testDeleteWhitespace() {
assertEquals(null, StringUtils.deleteWhitespace(null));
assertEquals("", StringUtils.deleteWhitespace(""));
assertEquals("", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B "));
assertEquals("", StringUtils.deleteWhitespace(StringUtilsTest.WHITESPACE));
assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.deleteWhitespace(StringUtilsTest.NON_WHITESPACE));
// Note: u-2007 and u-000A both cause problems in the source code
// it should ignore 2007 but delete 000A
assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F "));
assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F"));
assertEquals("test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt"));
}
public void testStrip_String() {
assertEquals(null, StringUtils.strip(null));
assertEquals("", StringUtils.strip(""));