scolebourne 2003/07/19 17:37:09
Modified: lang/src/test/org/apache/commons/lang
StringUtilsTrimEmptyTest.java
lang/src/java/org/apache/commons/lang StringUtils.java
Log:
Group all the trim/strip methods together in source file
Revision Changes Path
1.15 +34 -33
jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java
Index: StringUtilsTrimEmptyTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- StringUtilsTrimEmptyTest.java 20 Jul 2003 00:17:29 -0000 1.14
+++ StringUtilsTrimEmptyTest.java 20 Jul 2003 00:37:09 -0000 1.15
@@ -91,7 +91,39 @@
}
//-----------------------------------------------------------------------
+ public void testIsEmpty() {
+ assertEquals(true, StringUtils.isEmpty(null));
+ assertEquals(true, StringUtils.isEmpty(""));
+ assertEquals(false, StringUtils.isEmpty(" "));
+ assertEquals(false, StringUtils.isEmpty("foo"));
+ assertEquals(false, StringUtils.isEmpty(" foo "));
+ }
+
+ public void testIsNotEmpty() {
+ assertEquals(false, StringUtils.isNotEmpty(null));
+ assertEquals(false, StringUtils.isNotEmpty(""));
+ assertEquals(true, StringUtils.isNotEmpty(" "));
+ assertEquals(true, StringUtils.isNotEmpty("foo"));
+ assertEquals(true, StringUtils.isNotEmpty(" foo "));
+ }
+
+ public void testIsBlank() {
+ assertEquals(true, StringUtils.isBlank(null));
+ assertEquals(true, StringUtils.isBlank(""));
+ assertEquals(true, StringUtils.isBlank(StringUtilsTest.WHITESPACE));
+ assertEquals(false, StringUtils.isBlank("foo"));
+ assertEquals(false, StringUtils.isBlank(" foo "));
+ }
+
+ public void testIsNotBlank() {
+ assertEquals(false, StringUtils.isNotBlank(null));
+ assertEquals(false, StringUtils.isNotBlank(""));
+ assertEquals(false, StringUtils.isNotBlank(StringUtilsTest.WHITESPACE));
+ assertEquals(true, StringUtils.isNotBlank("foo"));
+ assertEquals(true, StringUtils.isNotBlank(" foo "));
+ }
+ //-----------------------------------------------------------------------
public void testClean() {
assertEquals(FOO, StringUtils.clean(FOO + " "));
assertEquals(FOO, StringUtils.clean(" " + FOO + " "));
@@ -138,38 +170,7 @@
assertEquals("", StringUtils.trimToEmpty(null));
}
- public void testIsEmpty() {
- assertEquals(true, StringUtils.isEmpty(null));
- assertEquals(true, StringUtils.isEmpty(""));
- assertEquals(false, StringUtils.isEmpty(" "));
- assertEquals(false, StringUtils.isEmpty("foo"));
- assertEquals(false, StringUtils.isEmpty(" foo "));
- }
-
- public void testIsNotEmpty() {
- assertEquals(false, StringUtils.isNotEmpty(null));
- assertEquals(false, StringUtils.isNotEmpty(""));
- assertEquals(true, StringUtils.isNotEmpty(" "));
- assertEquals(true, StringUtils.isNotEmpty("foo"));
- assertEquals(true, StringUtils.isNotEmpty(" foo "));
- }
-
- public void testIsBlank() {
- assertEquals(true, StringUtils.isBlank(null));
- assertEquals(true, StringUtils.isBlank(""));
- assertEquals(true, StringUtils.isBlank(StringUtilsTest.WHITESPACE));
- assertEquals(false, StringUtils.isBlank("foo"));
- assertEquals(false, StringUtils.isBlank(" foo "));
- }
-
- public void testIsNotBlank() {
- assertEquals(false, StringUtils.isNotBlank(null));
- assertEquals(false, StringUtils.isNotBlank(""));
- assertEquals(false, StringUtils.isNotBlank(StringUtilsTest.WHITESPACE));
- assertEquals(true, StringUtils.isNotBlank("foo"));
- assertEquals(true, StringUtils.isNotBlank(" foo "));
- }
-
+ //-----------------------------------------------------------------------
public void testStrip_String() {
assertEquals(null, StringUtils.strip(null));
assertEquals("", StringUtils.strip(""));
1.70 +321 -318
jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
Index: StringUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -r1.69 -r1.70
--- StringUtils.java 20 Jul 2003 00:17:29 -0000 1.69
+++ StringUtils.java 20 Jul 2003 00:37:09 -0000 1.70
@@ -147,7 +147,106 @@
public StringUtils() {
}
- // Empty
+ // Empty checks
+ //-----------------------------------------------------------------------
+
+ /**
+ * <p>Checks if a String is empty ("") or null.
+ * <code>null</code> returns <code>false</code></p>
+ *
+ * <pre>
+ * StringUtils.isEmpty(null) = true
+ * StringUtils.isEmpty("") = true
+ * StringUtils.isEmpty(" ") = false
+ * StringUtils.isEmpty("bob") = false
+ * StringUtils.isEmpty(" bob ") = false
+ * </pre>
+ *
+ * <p>NOTE: This method changed in version 2.0.
+ * It no longer trims the String.
+ * That functionality is available in isBlank().</p>
+ *
+ * @param str the String to check, may be null
+ * @return <code>true</code> if the String is empty or null
+ */
+ public static boolean isEmpty(String str) {
+ return (str == null || str.length() == 0);
+ }
+
+ /**
+ * <p>Checks if a String is not empty ("") and not null.</p>
+ *
+ * <pre>
+ * StringUtils.isNotEmpty(null) = false
+ * StringUtils.isNotEmpty("") = false
+ * StringUtils.isNotEmpty(" ") = true
+ * StringUtils.isNotEmpty("bob") = true
+ * StringUtils.isNotEmpty(" bob ") = true
+ * </pre>
+ *
+ * @param str the String to check, may be null
+ * @return <code>true</code> if the String is not empty and not null
+ */
+ public static boolean isNotEmpty(String str) {
+ return (str != null && str.length() > 0);
+ }
+
+ /**
+ * <p>Checks if a String is whitespace, empty ("") or null.</p>
+ *
+ * <pre>
+ * StringUtils.isBlank(null) = true
+ * StringUtils.isBlank("") = true
+ * StringUtils.isBlank(" ") = true
+ * StringUtils.isBlank("bob") = false
+ * StringUtils.isBlank(" bob ") = false
+ * </pre>
+ *
+ * @param str the String to check, may be null
+ * @return <code>true</code> if the String is null, empty or whitespace
+ */
+ public static boolean isBlank(String str) {
+ int strLen;
+ if (str == null || (strLen = str.length()) == 0) {
+ return true;
+ }
+ for (int i = 0; i < strLen; i++) {
+ if ((Character.isWhitespace(str.charAt(i)) == false) ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * <p>Checks if a String is not empty ("") and not null.</p>
+ *
+ * <pre>
+ * StringUtils.isNotBlank(null) = false
+ * StringUtils.isNotBlank("") = false
+ * StringUtils.isNotBlank(" ") = false
+ * StringUtils.isNotBlank("bob") = true
+ * StringUtils.isNotBlank(" bob ") = true
+ * </pre>
+ *
+ * @param str the String to check, may be null
+ * @return <code>true</code> if the String is
+ * not empty and not null and not whitespace
+ */
+ public static boolean isNotBlank(String str) {
+ int strLen;
+ if (str == null || (strLen = str.length()) == 0) {
+ return false;
+ }
+ for (int i = 0; i < strLen; i++) {
+ if ((Character.isWhitespace(str.charAt(i)) == false) ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ // Trim
//-----------------------------------------------------------------------
/**
@@ -250,104 +349,262 @@
return (str == null ? "" : str.trim());
}
- // Empty checks
+ // Stripping
//-----------------------------------------------------------------------
-
+
/**
- * <p>Checks if a String is empty ("") or null.
- * <code>null</code> returns <code>false</code></p>
+ * <p>Strips whitespace from the start and end of a String.</p>
+ *
+ * <p>This is similar to [EMAIL PROTECTED] #trim(String)} but removes
whitespace.
+ * Whitespace is defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
+ *
+ * <p>A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
- * StringUtils.isEmpty(null) = true
- * StringUtils.isEmpty("") = true
- * StringUtils.isEmpty(" ") = false
- * StringUtils.isEmpty("bob") = false
- * StringUtils.isEmpty(" bob ") = false
+ * 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(" ab c ") = "ab c"
* </pre>
- *
- * <p>NOTE: This method changed in version 2.0.
- * It no longer trims the String.
- * That functionality is available in isBlank().</p>
*
- * @param str the String to check, may be null
- * @return <code>true</code> if the String is empty or null
+ * @param str the String to remove whitespace from, may be null
+ * @return the stripped String, <code>null</code> if null String input
*/
- public static boolean isEmpty(String str) {
- return (str == null || str.length() == 0);
+ 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 [EMAIL PROTECTED] #trimToNull(String)} but removes
whitespace.
+ * Whitespace is defined by [EMAIL PROTECTED] 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>Checks if a String is not empty ("") and not null.</p>
+ /**
+ * <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 [EMAIL PROTECTED] #trimToEmpty(String)} but removes
whitespace.
+ * Whitespace is defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
*
* <pre>
- * StringUtils.isNotEmpty(null) = false
- * StringUtils.isNotEmpty("") = false
- * StringUtils.isNotEmpty(" ") = true
- * StringUtils.isNotEmpty("bob") = true
- * StringUtils.isNotEmpty(" bob ") = true
+ * 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 [EMAIL PROTECTED] String#trim()} but allows the characters
+ * to be stripped to be controlled.</p>
*
- * @param str the String to check, may be null
- * @return <code>true</code> if the String is not empty and not null
+ * <p>A <code>null</code> input String returns <code>null</code>.</p>
+ *
+ * <p>If the stripChars String is <code>null</code>, whitespace is
+ * stripped as defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.
+ * Alternatively use [EMAIL PROTECTED] #strip(String)}.</p>
+ *
+ * <pre>
+ * StringUtils.strip(null, null) = null
+ * StringUtils.strip("abc", null) = "abc"
+ * StringUtils.strip(" abc", null) = "abc"
+ * StringUtils.strip("abc ", null) = "abc"
+ * StringUtils.strip(" abc ", null) = "abc"
+ * StringUtils.strip(" abcyx", "xyz") = " abc"
+ * </pre>
+ *
+ * @param str the String to remove characters from, may be null
+ * @param stripChars the characters to remove, null treated as whitespace
+ * @return the stripped String, <code>null</code> if null String input
*/
- public static boolean isNotEmpty(String str) {
- return (str != null && str.length() > 0);
+ public static String strip(String str, String stripChars) {
+ if (str == null || str.length() == 0) {
+ return str;
+ }
+ str = stripStart(str, stripChars);
+ return stripEnd(str, stripChars);
}
/**
- * <p>Checks if a String is whitespace, empty ("") or null.</p>
+ * <p>Strips any of a set of characters from the start of a String.</p>
+ *
+ * <p>A <code>null</code> input String returns <code>null</code>.</p>
+ *
+ * <p>If the stripChars String is <code>null</code>, whitespace is
+ * stripped as defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
*
* <pre>
- * StringUtils.isBlank(null) = true
- * StringUtils.isBlank("") = true
- * StringUtils.isBlank(" ") = true
- * StringUtils.isBlank("bob") = false
- * StringUtils.isBlank(" bob ") = false
+ * StringUtils.strip(null, null) = null
+ * StringUtils.strip("abc", null) = "abc"
+ * StringUtils.strip(" abc", null) = "abc"
+ * StringUtils.strip("abc ", null) = "abc "
+ * StringUtils.strip(" abc ", null) = "abc "
+ * StringUtils.strip("yxabc ", "xyz") = "abc "
* </pre>
- *
- * @param str the String to check, may be null
- * @return <code>true</code> if the String is null, empty or whitespace
+ *
+ * @param str the String to remove characters from, may be null
+ * @param stripChars the characters to remove, null treated as whitespace
+ * @return the stripped String, <code>null</code> if null String input
*/
- public static boolean isBlank(String str) {
+ public static String stripStart(String str, String stripChars) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
- return true;
+ return str;
}
- for (int i = 0; i < strLen; i++) {
- if ((Character.isWhitespace(str.charAt(i)) == false) ) {
- return false;
+ int start = 0;
+ if (stripChars == null) {
+ while ((start != strLen) && Character.isWhitespace(str.charAt(start))) {
+ start++;
+ }
+ } else {
+ while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) !=
-1)) {
+ start++;
}
}
- return true;
+ return str.substring(start);
}
/**
- * <p>Checks if a String is not empty ("") and not null.</p>
+ * <p>Strips any of a set of characters from the end of a String.</p>
+ *
+ * <p>A <code>null</code> input String returns <code>null</code>.</p>
+ *
+ * <p>If the stripChars String is <code>null</code>, whitespace is
+ * stripped as defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
*
* <pre>
- * StringUtils.isNotBlank(null) = false
- * StringUtils.isNotBlank("") = false
- * StringUtils.isNotBlank(" ") = false
- * StringUtils.isNotBlank("bob") = true
- * StringUtils.isNotBlank(" bob ") = true
+ * StringUtils.strip(null, null) = null
+ * StringUtils.strip("abc", null) = "abc"
+ * StringUtils.strip(" abc", null) = " abc"
+ * StringUtils.strip("abc ", null) = "abc"
+ * StringUtils.strip(" abc ", null) = " abc"
+ * StringUtils.strip(" abcyx", "xyz") = " abc"
* </pre>
- *
- * @param str the String to check, may be null
- * @return <code>true</code> if the String is
- * not empty and not null and not whitespace
+ *
+ * @param str the String to remove characters from, may be null
+ * @param stripChars the characters to remove, null treated as whitespace
+ * @return the stripped String, <code>null</code> if null String input
*/
- public static boolean isNotBlank(String str) {
- int strLen;
- if (str == null || (strLen = str.length()) == 0) {
- return false;
+ public static String stripEnd(String str, String stripChars) {
+ int end;
+ if (str == null || (end = str.length()) == 0) {
+ return str;
}
- for (int i = 0; i < strLen; i++) {
- if ((Character.isWhitespace(str.charAt(i)) == false) ) {
- return true;
+
+ if (stripChars == null) {
+ while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {
+ end--;
+ }
+ } else {
+ while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
+ end--;
}
}
- return false;
- }
+ return str.substring(0, end);
+ }
+
+ // StripAll
+ //-----------------------------------------------------------------------
+
+ /**
+ * <p>Strips whitespace from the start and end of every String in an array.
+ * Whitespace is defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
+ *
+ * <p>A new array is returned each time, except for length zero.
+ * A <code>null</code> array will return <code>null</code>.
+ * An empty array will return itself.
+ * A <code>null</code> array entry will be ignored.</p>
+ *
+ * <pre>
+ * StringUtils.stripAll(null) = null
+ * StringUtils.stripAll([]) = []
+ * StringUtils.stripAll(["abc", " abc"]) = ["abc", "abc"]
+ * StringUtils.stripAll(["abc ", null]) = ["abc", null]
+ * </pre>
+ *
+ * @param str the array to remove whitespace from, may be null
+ * @return the stripped Strings, <code>null</code> if null array input
+ */
+ public static String[] stripAll(String[] strs) {
+ return stripAll(strs, null);
+ }
+
+ /**
+ * <p>Strips any of a set of characters from the start and end of every
+ * String in an array.</p>
+ * Whitespace is defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
+ *
+ * <p>A new array is returned each time, except for length zero.
+ * A <code>null</code> array will return <code>null</code>.
+ * An empty array will return itself.
+ * A <code>null</code> array entry will be ignored.
+ * A <code>null</code> stripChars will strip whitespace as defined by
+ * [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
+ *
+ * <pre>
+ * StringUtils.stripAll(null, null) = null
+ * StringUtils.stripAll([], null) = []
+ * StringUtils.stripAll(["abc", " abc"], null) = ["abc", "abc"]
+ * StringUtils.stripAll(["abc ", null], null) = ["abc", null]
+ * StringUtils.stripAll(["abc ", null], "yz") = ["abc ", null]
+ * StringUtils.stripAll(["yabcz", null], "yz") = ["abc", null]
+ * </pre>
+ *
+ * @param str the array to remove characters from, may be null
+ * @param stripChars the characters to remove, null treated as whitespace
+ * @return the stripped Strings, <code>null</code> if null array input
+ */
+ public static String[] stripAll(String[] strs, String stripChars) {
+ int strsLen;
+ if (strs == null || (strsLen = strs.length) == 0) {
+ return strs;
+ }
+ String[] newArr = new String[strsLen];
+ for (int i = 0; i < strsLen; i++) {
+ newArr[i] = strip(strs[i], stripChars);
+ }
+ return newArr;
+ }
// Equals
//-----------------------------------------------------------------------
@@ -2507,260 +2764,6 @@
str = rightPad(str, size, padStr);
return str;
}
-
- // Stripping
- //-----------------------------------------------------------------------
-
- /**
- * <p>Strips whitespace from the start and end of a String.</p>
- *
- * <p>This is similar to [EMAIL PROTECTED] String#trim()} but instead removes
whitespace.
- * Whitespace is defined by [EMAIL PROTECTED] 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"
- * StringUtils.strip(" abc ") = "abc"
- * StringUtils.strip(" ab c ") = "ab c"
- * </pre>
- *
- * @param str the String to remove whitespace from, may be null
- * @return the stripped String, <code>null</code> if null String input
- */
- 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 [EMAIL PROTECTED] #trimToNull()} but instead removes
whitespace.
- * Whitespace is defined by [EMAIL PROTECTED] 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 [EMAIL PROTECTED] #trimToEmpty()} but instead removes
whitespace.
- * Whitespace is defined by [EMAIL PROTECTED] 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 [EMAIL PROTECTED] String#trim()} but allows the characters
- * to be stripped to be controlled.</p>
- *
- * <p>A <code>null</code> input String returns <code>null</code>.</p>
- *
- * <p>If the stripChars String is <code>null</code>, whitespace is
- * stripped as defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.
- * Alternatively use [EMAIL PROTECTED] #strip(String)}.</p>
- *
- * <pre>
- * StringUtils.strip(null, null) = null
- * StringUtils.strip("abc", null) = "abc"
- * StringUtils.strip(" abc", null) = "abc"
- * StringUtils.strip("abc ", null) = "abc"
- * StringUtils.strip(" abc ", null) = "abc"
- * StringUtils.strip(" abcyx", "xyz") = " abc"
- * </pre>
- *
- * @param str the String to remove characters from, may be null
- * @param stripChars the characters to remove, null treated as whitespace
- * @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);
- }
-
- /**
- * <p>Strips any of a set of characters from the start of a String.</p>
- *
- * <p>A <code>null</code> input String returns <code>null</code>.</p>
- *
- * <p>If the stripChars String is <code>null</code>, whitespace is
- * stripped as defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
- *
- * <pre>
- * StringUtils.strip(null, null) = null
- * StringUtils.strip("abc", null) = "abc"
- * StringUtils.strip(" abc", null) = "abc"
- * StringUtils.strip("abc ", null) = "abc "
- * StringUtils.strip(" abc ", null) = "abc "
- * StringUtils.strip("yxabc ", "xyz") = "abc "
- * </pre>
- *
- * @param str the String to remove characters from, may be null
- * @param stripChars the characters to remove, null treated as whitespace
- * @return the stripped String, <code>null</code> if null String input
- */
- public static String stripStart(String str, String stripChars) {
- int strLen;
- if (str == null || (strLen = str.length()) == 0) {
- return str;
- }
- int start = 0;
- if (stripChars == null) {
- while ((start != strLen) && Character.isWhitespace(str.charAt(start))) {
- start++;
- }
- } else {
- while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) !=
-1)) {
- start++;
- }
- }
- return str.substring(start);
- }
-
- /**
- * <p>Strips any of a set of characters from the end of a String.</p>
- *
- * <p>A <code>null</code> input String returns <code>null</code>.</p>
- *
- * <p>If the stripChars String is <code>null</code>, whitespace is
- * stripped as defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
- *
- * <pre>
- * StringUtils.strip(null, null) = null
- * StringUtils.strip("abc", null) = "abc"
- * StringUtils.strip(" abc", null) = " abc"
- * StringUtils.strip("abc ", null) = "abc"
- * StringUtils.strip(" abc ", null) = " abc"
- * StringUtils.strip(" abcyx", "xyz") = " abc"
- * </pre>
- *
- * @param str the String to remove characters from, may be null
- * @param stripChars the characters to remove, null treated as whitespace
- * @return the stripped String, <code>null</code> if null String input
- */
- public static String stripEnd(String str, String stripChars) {
- int end;
- if (str == null || (end = str.length()) == 0) {
- return str;
- }
-
- if (stripChars == null) {
- while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {
- end--;
- }
- } else {
- while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
- end--;
- }
- }
- return str.substring(0, end);
- }
-
- /**
- * <p>Strips whitespace from the start and end of every String in an array.
- * Whitespace is defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
- *
- * <p>A new array is returned each time, except for length zero.
- * A <code>null</code> array will return <code>null</code>.
- * An empty array will return itself.
- * A <code>null</code> array entry will be ignored.</p>
- *
- * <pre>
- * StringUtils.stripAll(null) = null
- * StringUtils.stripAll([]) = []
- * StringUtils.stripAll(["abc", " abc"]) = ["abc", "abc"]
- * StringUtils.stripAll(["abc ", null]) = ["abc", null]
- * </pre>
- *
- * @param str the array to remove whitespace from, may be null
- * @return the stripped Strings, <code>null</code> if null array input
- */
- public static String[] stripAll(String[] strs) {
- return stripAll(strs, null);
- }
-
- /**
- * <p>Strips any of a set of characters from the start and end of every
- * String in an array.</p>
- * Whitespace is defined by [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
- *
- * <p>A new array is returned each time, except for length zero.
- * A <code>null</code> array will return <code>null</code>.
- * An empty array will return itself.
- * A <code>null</code> array entry will be ignored.
- * A <code>null</code> stripChars will strip whitespace as defined by
- * [EMAIL PROTECTED] Character#isWhitespace(char)}.</p>
- *
- * <pre>
- * StringUtils.stripAll(null, null) = null
- * StringUtils.stripAll([], null) = []
- * StringUtils.stripAll(["abc", " abc"], null) = ["abc", "abc"]
- * StringUtils.stripAll(["abc ", null], null) = ["abc", null]
- * StringUtils.stripAll(["abc ", null], "yz") = ["abc ", null]
- * StringUtils.stripAll(["yabcz", null], "yz") = ["abc", null]
- * </pre>
- *
- * @param str the array to remove characters from, may be null
- * @param stripChars the characters to remove, null treated as whitespace
- * @return the stripped Strings, <code>null</code> if null array input
- */
- public static String[] stripAll(String[] strs, String stripChars) {
- int strsLen;
- if (strs == null || (strsLen = strs.length) == 0) {
- return strs;
- }
- String[] newArr = new String[strsLen];
- for (int i = 0; i < strsLen; i++) {
- newArr[i] = strip(strs[i], stripChars);
- }
- return newArr;
- }
// Case conversion
//-----------------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]