Changed the deleteWhitespace method to delete according to Character.isWhitespce

Renamed the existing deleteWhitespace functionality to deleteSpaces.
This is in accordance with Character.isSpace.

Submitted by:	Steve Downey's idea


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137068 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2002-09-27 06:08:16 +00:00
parent d58545381f
commit e0722ed784
2 changed files with 40 additions and 7 deletions

View File

@ -73,7 +73,7 @@ import java.util.Iterator;
* @author <a href="mailto:rand_mcneely@yahoo.com>Rand McNeely</a>
* @author <a href="mailto:scolebourne@joda.org>Stephen Colebourne</a>
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
* @version $Id: StringUtils.java,v 1.14 2002/09/27 05:53:37 bayard Exp $
* @version $Id: StringUtils.java,v 1.15 2002/09/27 06:08:16 bayard Exp $
*/
public class StringUtils {
@ -118,16 +118,36 @@ public class StringUtils {
return (str == null ? null : str.trim());
}
/**
* Deletes all 'space' characters from a String.
* Spaces are defined as {' ', '\t', '\r', '\n', '\b'}
* in line with the deprecated Character.isSpace
*
* @param str String target to delete spaces from
* @return the text without spaces
* @throws NullPointerException
*/
public static String deleteSpaces(String str) {
return CharSetUtils.delete(str, " \t\r\n\b");
}
/**
* Deletes all whitespace from a String.
* Whitespace is defined as {' ', '\t', '\r', '\n', '\b'}
* Whitespace is defined by Character.isWhitespace
*
* @param str String target to delete whitespace from
* @return the text without whitespace
* @throws NullPointerException
*/
public static String deleteWhitespace(String str) {
return CharSetUtils.delete(str, " \t\r\n\b");
StringBuffer buffer = new StringBuffer();
int sz = str.length();
for (int i=0; i<sz; i++) {
if(!Character.isWhitespace(str.charAt(i))) {
buffer.append(str.charAt(i));
}
}
return buffer.toString();
}
/**

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.4 2002/07/21 20:19:50 bayard Exp $
* @version $Id: StringUtilsTrimEmptyTest.java,v 1.5 2002/09/27 06:08:16 bayard Exp $
*/
public class StringUtilsTrimEmptyTest extends TestCase {
private static final String FOO = "foo";
@ -122,13 +122,26 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(true, StringUtils.isEmpty(null));
}
public void testDeleteWhitespace() {
public void testDeleteSpace() {
assertEquals("deleteWhitespace(String) failed",
"", StringUtils.deleteWhitespace(""));
assertEquals("deleteWhitespace(String) failed",
"", StringUtils.deleteWhitespace(" \t\t\n\n "));
"", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B "));
// Note: u-2007 and u-000A both cause problems in the source code
// it should ignore 2007 but delete 000A
assertEquals("deleteWhitespace(String) failed",
"test", StringUtils.deleteWhitespace("t \t\ne\rs\n\n \tt"));
"\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F "));
assertEquals("deleteWhitespace(String) failed",
"\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F"));
assertEquals("deleteWhitespace(String) failed",
"test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt"));
assertEquals("deleteSpaces(String) failed",
"", StringUtils.deleteSpaces(""));
assertEquals("deleteSpaces(String) failed",
"", StringUtils.deleteSpaces(" \t\t\n\n "));
assertEquals("deleteSpaces(String) failed",
"test", StringUtils.deleteSpaces("t \t\ne\rs\n\n \tt"));
}
public void testStrip() {