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:
parent
51af2aef0b
commit
97adbab600
|
@ -98,7 +98,7 @@
|
||||||
* @author Arun Mammen Thomas
|
* @author Arun Mammen Thomas
|
||||||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||||
* @since 1.0
|
* @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 {
|
public class StringUtils {
|
||||||
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
|
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
|
||||||
|
@ -179,7 +179,8 @@ public static String clean(String str) {
|
||||||
* <code>null</code>.</p>
|
* <code>null</code>.</p>
|
||||||
*
|
*
|
||||||
* <p>The String is trimmed using {@link String#trim()}.
|
* <p>The String is trimmed using {@link String#trim()}.
|
||||||
* Trim removes start and end characters <= 32.</p>
|
* Trim removes start and end characters <= 32.
|
||||||
|
* To strip whitespace use {@link #strip(String)}.</p>
|
||||||
*
|
*
|
||||||
* <p>To trim your choice of characters, use the
|
* <p>To trim your choice of characters, use the
|
||||||
* {@link #strip(String, String)} methods.</p>
|
* {@link #strip(String, String)} methods.</p>
|
||||||
|
@ -192,7 +193,6 @@ public static String clean(String str) {
|
||||||
* StringUtils.trim("") = ""
|
* StringUtils.trim("") = ""
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @see java.lang.String#trim()
|
|
||||||
* @param str the String to be trimmed, may be null
|
* @param str the String to be trimmed, may be null
|
||||||
* @return the trimmed string, <code>null</code> if null String input
|
* @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>.
|
* empty ("") after the trim or if it is <code>null</code>.
|
||||||
*
|
*
|
||||||
* <p>The String is trimmed using {@link String#trim()}.
|
* <p>The String is trimmed using {@link String#trim()}.
|
||||||
* Trim removes start and end characters <= 32.</p>
|
* Trim removes start and end characters <= 32.
|
||||||
|
* To strip whitespace use {@link #stripToNull(String)}.</p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* StringUtils.trimToNull(null) = null
|
* StringUtils.trimToNull(null) = null
|
||||||
|
@ -216,7 +217,6 @@ public static String trim(String str) {
|
||||||
* StringUtils.trimToNull("") = null
|
* StringUtils.trimToNull("") = null
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @see java.lang.String#trim()
|
|
||||||
* @param str the String to be trimmed, may be null
|
* @param str the String to be trimmed, may be null
|
||||||
* @return the trimmed String,
|
* @return the trimmed String,
|
||||||
* <code>null</code> if only chars <= 32, empty or null String input
|
* <code>null</code> 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 <code>null</code>.
|
* is empty ("") after the trim or if it is <code>null</code>.
|
||||||
*
|
*
|
||||||
* <p>The String is trimmed using {@link String#trim()}.
|
* <p>The String is trimmed using {@link String#trim()}.
|
||||||
* Trim removes start and end characters <= 32.</p>
|
* Trim removes start and end characters <= 32.
|
||||||
|
* To strip whitespace use {@link #stripToEmpty(String)}.</p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* StringUtils.trimToEmpty(null) = ""
|
* StringUtils.trimToEmpty(null) = ""
|
||||||
|
@ -242,7 +243,6 @@ public static String trimToNull(String str) {
|
||||||
* StringUtils.trimToEmpty("") = ""
|
* StringUtils.trimToEmpty("") = ""
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @see java.lang.String#trim()
|
|
||||||
* @param str the String to be trimmed, may be null
|
* @param str the String to be trimmed, may be null
|
||||||
* @return the trimmed String, or an empty String if <code>null</code> input
|
* @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.
|
* <p>Strips whitespace from the start and end of a String.</p>
|
||||||
* This is similar to {@link String#trim()} but instead removes whitespace.
|
*
|
||||||
|
* <p>This is similar to {@link String#trim()} but instead removes whitespace.
|
||||||
* Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
|
* Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
|
||||||
*
|
*
|
||||||
* <p>A <code>null</code> input String returns <code>null</code>.</p>
|
* <p>A <code>null</code> input String returns <code>null</code>.</p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* StringUtils.strip(null) = null
|
* StringUtils.strip(null) = null
|
||||||
|
* StringUtils.strip("") = ""
|
||||||
|
* StringUtils.strip(" ") = ""
|
||||||
* StringUtils.strip("abc") = "abc"
|
* StringUtils.strip("abc") = "abc"
|
||||||
* StringUtils.strip(" abc") = "abc"
|
* 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);
|
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.
|
* <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
|
* 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
|
* @return the stripped String, <code>null</code> if null String input
|
||||||
*/
|
*/
|
||||||
public static String strip(String str, String stripChars) {
|
public static String strip(String str, String stripChars) {
|
||||||
|
if (str == null || str.length() == 0) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
str = stripStart(str, stripChars);
|
str = stripStart(str, stripChars);
|
||||||
return stripEnd(str, stripChars);
|
return stripEnd(str, stripChars);
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</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 {
|
public class StringUtilsTrimEmptyTest extends TestCase {
|
||||||
private static final String FOO = "foo";
|
private static final String FOO = "foo";
|
||||||
|
@ -199,6 +199,26 @@ public void testStrip_String() {
|
||||||
StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE));
|
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() {
|
public void testStrip_StringString() {
|
||||||
// null strip
|
// null strip
|
||||||
assertEquals(null, StringUtils.strip(null, null));
|
assertEquals(null, StringUtils.strip(null, null));
|
||||||
|
|
Loading…
Reference in New Issue