diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java
index 46702b87f..469a37ef7 100644
--- a/src/java/org/apache/commons/lang/StringUtils.java
+++ b/src/java/org/apache/commons/lang/StringUtils.java
@@ -98,7 +98,7 @@
* @author Arun Mammen Thomas
* @author Gary Gregory
* @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) {
* null
.
The String is trimmed using {@link String#trim()}. - * Trim removes start and end characters <= 32.
+ * Trim removes start and end characters <= 32. + * To strip whitespace use {@link #strip(String)}. * *To trim your choice of characters, use the * {@link #strip(String, String)} methods.
@@ -192,7 +193,6 @@ public static String clean(String str) { * StringUtils.trim("") = "" * * - * @see java.lang.String#trim() * @param str the String to be trimmed, may be null * @return the trimmed string,null
if null String input
*/
@@ -206,7 +206,8 @@ public static String trim(String str) {
* empty ("") after the trim or if it is null
.
*
* The String is trimmed using {@link String#trim()}. - * Trim removes start and end characters <= 32.
+ * Trim removes start and end characters <= 32. + * To strip whitespace use {@link #stripToNull(String)}. * ** StringUtils.trimToNull(null) = null @@ -216,7 +217,6 @@ public static String trim(String str) { * StringUtils.trimToNull("") = null ** - * @see java.lang.String#trim() * @param str the String to be trimmed, may be null * @return the trimmed String, *
null
if only chars <= 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 null
.
*
* The String is trimmed using {@link String#trim()}. - * Trim removes start and end characters <= 32.
+ * Trim removes start and end characters <= 32. + * To strip whitespace use {@link #stripToEmpty(String)}. * ** StringUtils.trimToEmpty(null) = "" @@ -242,7 +243,6 @@ public static String trimToNull(String str) { * StringUtils.trimToEmpty("") = "" ** - * @see java.lang.String#trim() * @param str the String to be trimmed, may be null * @return the trimmed String, or an empty String if
null
input
*/
@@ -2510,14 +2510,17 @@ public static String center(String str, int size, String padStr) {
//-----------------------------------------------------------------------
/**
- * Strips whitespace from the start and end of a String. - * This is similar to {@link String#trim()} but instead removes whitespace. + *
Strips whitespace from the start and end of a String.
+ * + *This is similar to {@link String#trim()} but instead removes whitespace. * Whitespace is defined by {@link Character#isWhitespace(char)}.
* *A null
input String returns null
.
* 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); } + /** + *Strips whitespace from the start and end of a String returning + *
+ * + *null
if the String is empty ("") after the strip.This is similar to {@link #trimToNull()} but instead removes whitespace. + * Whitespace is defined by {@link Character#isWhitespace(char)}.
+ * + *+ * 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" + *+ * + * @param str the String to be stripped, may be null + * @return the stripped String, + *null
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); + } + + /** + *Strips whitespace from the start and end of a String returning + * an empty String if
+ * + *null
input.This is similar to {@link #trimToEmpty()} but instead removes whitespace. + * Whitespace is defined by {@link Character#isWhitespace(char)}.
+ * + *+ * 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" + *+ * + * @param str the String to be stripped, may be null + * @return the trimmed String, or an empty String ifnull
input + */ + public static String stripToEmpty(String str) { + return (str == null ? "" : strip(str, null)); + } + /** *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,
null
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); } diff --git a/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java b/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java index 596ef0ecf..b600ae5e3 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java @@ -63,7 +63,7 @@ * * @author Stephen Colebourne * @author Ringo De Smet - * @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));