Add new methods stripToNull/stripToEmpty to provide alternative to trim versions

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137462 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-20 00:04:12 +00:00
parent 51af2aef0b
commit 97adbab600
2 changed files with 91 additions and 10 deletions

View File

@ -98,7 +98,7 @@
* @author Arun Mammen Thomas
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 1.0
* @version $Id: StringUtils.java,v 1.67 2003/07/19 23:29:06 scolebourne Exp $
* @version $Id: StringUtils.java,v 1.68 2003/07/20 00:04:12 scolebourne Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -179,7 +179,8 @@ public static String clean(String str) {
* <code>null</code>.</p>
*
* <p>The String is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
* Trim removes start and end characters &lt;= 32.
* To strip whitespace use {@link #strip(String)}.</p>
*
* <p>To trim your choice of characters, use the
* {@link #strip(String, String)} methods.</p>
@ -192,7 +193,6 @@ public static String clean(String str) {
* StringUtils.trim("") = ""
* </pre>
*
* @see java.lang.String#trim()
* @param str the String to be trimmed, may be null
* @return the trimmed string, <code>null</code> if null String input
*/
@ -206,7 +206,8 @@ public static String trim(String str) {
* empty ("") after the trim or if it is <code>null</code>.
*
* <p>The String is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
* Trim removes start and end characters &lt;= 32.
* To strip whitespace use {@link #stripToNull(String)}.</p>
*
* <pre>
* StringUtils.trimToNull(null) = null
@ -216,7 +217,6 @@ public static String trim(String str) {
* StringUtils.trimToNull("") = null
* </pre>
*
* @see java.lang.String#trim()
* @param str the String to be trimmed, may be null
* @return the trimmed String,
* <code>null</code> if only chars &lt;= 32, empty or null String input
@ -232,7 +232,8 @@ public static String trimToNull(String str) {
* is empty ("") after the trim or if it is <code>null</code>.
*
* <p>The String is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
* Trim removes start and end characters &lt;= 32.
* To strip whitespace use {@link #stripToEmpty(String)}.</p>
*
* <pre>
* StringUtils.trimToEmpty(null) = ""
@ -242,7 +243,6 @@ public static String trimToNull(String str) {
* StringUtils.trimToEmpty("") = ""
* </pre>
*
* @see java.lang.String#trim()
* @param str the String to be trimmed, may be null
* @return the trimmed String, or an empty String if <code>null</code> input
*/
@ -2510,14 +2510,17 @@ public static String center(String str, int size, String padStr) {
//-----------------------------------------------------------------------
/**
* <p>Strips whitespace from the start and end of a String.
* This is similar to {@link String#trim()} but instead removes whitespace.
* <p>Strips whitespace from the start and end of a String.</p>
*
* <p>This is similar to {@link String#trim()} but instead removes whitespace.
* Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.strip(null) = null
* StringUtils.strip("") = ""
* StringUtils.strip(" ") = ""
* StringUtils.strip("abc") = "abc"
* StringUtils.strip(" abc") = "abc"
* StringUtils.strip("abc ") = "abc"
@ -2532,6 +2535,61 @@ public static String strip(String str) {
return strip(str, null);
}
/**
* <p>Strips whitespace from the start and end of a String returning
* <code>null</code> if the String is empty ("") after the strip.</p>
*
* <p>This is similar to {@link #trimToNull()} but instead removes whitespace.
* Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
*
* <pre>
* StringUtils.strip(null) = null
* StringUtils.strip("") = null
* StringUtils.strip(" ") = null
* StringUtils.strip("abc") = "abc"
* StringUtils.strip(" abc") = "abc"
* StringUtils.strip("abc ") = "abc"
* StringUtils.strip(" abc ") = "abc"
* StringUtils.strip(" ab c ") = "ab c"
* </pre>
*
* @param str the String to be stripped, may be null
* @return the stripped String,
* <code>null</code> if whitespace, empty or null String input
*/
public static String stripToNull(String str) {
if (str == null) {
return null;
}
str = strip(str, null);
return (str.length() == 0 ? null : str);
}
/**
* <p>Strips whitespace from the start and end of a String returning
* an empty String if <code>null</code> input.</p>
*
* <p>This is similar to {@link #trimToEmpty()} but instead removes whitespace.
* Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
*
* <pre>
* StringUtils.strip(null) = ""
* StringUtils.strip("") = ""
* StringUtils.strip(" ") = ""
* StringUtils.strip("abc") = "abc"
* StringUtils.strip(" abc") = "abc"
* StringUtils.strip("abc ") = "abc"
* StringUtils.strip(" abc ") = "abc"
* StringUtils.strip(" ab c ") = "ab c"
* </pre>
*
* @param str the String to be stripped, may be null
* @return the trimmed String, or an empty String if <code>null</code> input
*/
public static String stripToEmpty(String str) {
return (str == null ? "" : strip(str, null));
}
/**
* <p>Strips any of a set of characters from the start and end of a String.
* This is similar to {@link String#trim()} but allows the characters
@ -2557,6 +2615,9 @@ public static String strip(String str) {
* @return the stripped String, <code>null</code> if null String input
*/
public static String strip(String str, String stripChars) {
if (str == null || str.length() == 0) {
return str;
}
str = stripStart(str, stripChars);
return stripEnd(str, stripChars);
}

View File

@ -63,7 +63,7 @@
*
* @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.12 2003/07/19 21:55:05 scolebourne Exp $
* @version $Id: StringUtilsTrimEmptyTest.java,v 1.13 2003/07/20 00:04:12 scolebourne Exp $
*/
public class StringUtilsTrimEmptyTest extends TestCase {
private static final String FOO = "foo";
@ -199,6 +199,26 @@ public void testStrip_String() {
StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE));
}
public void testStripToNull_String() {
assertEquals(null, StringUtils.stripToNull(null));
assertEquals(null, StringUtils.stripToNull(""));
assertEquals(null, StringUtils.stripToNull(" "));
assertEquals(null, StringUtils.stripToNull(StringUtilsTest.WHITESPACE));
assertEquals("ab c", StringUtils.stripToNull(" ab c "));
assertEquals(StringUtilsTest.NON_WHITESPACE,
StringUtils.stripToNull(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE));
}
public void testStripToEmpty_String() {
assertEquals("", StringUtils.stripToEmpty(null));
assertEquals("", StringUtils.stripToEmpty(""));
assertEquals("", StringUtils.stripToEmpty(" "));
assertEquals("", StringUtils.stripToEmpty(StringUtilsTest.WHITESPACE));
assertEquals("ab c", StringUtils.stripToEmpty(" ab c "));
assertEquals(StringUtilsTest.NON_WHITESPACE,
StringUtils.stripToEmpty(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE));
}
public void testStrip_StringString() {
// null strip
assertEquals(null, StringUtils.strip(null, null));