diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 08e29d904..9fae0f2fa 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -80,7 +80,9 @@ * - substring extraction relative to other strings *
Removes a substring only if it is at the begining of a source string, + * otherwise returns the source string.
+ * + *A null
source string will return null
.
+ * An empty ("") source string will return the empty string.
+ * A null
search string will return the source string.
+ * StringUtils.removeStart(null, *) = null + * StringUtils.removeStart("", *) = "" + * StringUtils.removeStart(*, null) = * + * StringUtils.removeStart("www.domain.com", "www.") = "domain.com" + * StringUtils.removeStart("domain.com", "www.") = "domain.com" + * StringUtils.removeStart("www.domain.com", "domain") = "www.domain.com" + * StringUtils.removeStart("abc", "") = "abc" + *+ * + * @param str the source String to search, may be null + * @param remove the String to search for and remove, may be null + * @return the substring with the string removed if found, + *
null
if null String input
+ * @since 2.1
+ */
+ public static String removeStart(String str, String remove) {
+ if (str == null || str.length() == 0 || remove == null || remove.length() == 0) {
+ return str;
+ }
+ if (str.startsWith(remove)){
+ return str.substring(remove.length());
+ }
+ return str;
+ }
+
+ /**
+ * Removes a substring only if it is at the end of a source string, + * otherwise returns the source string.
+ * + *A null
source string will return null
.
+ * An empty ("") source string will return the empty string.
+ * A null
search string will return the source string.
+ * StringUtils.removeEnd(null, *) = null + * StringUtils.removeEnd("", *) = "" + * StringUtils.removeEnd(*, null) = * + * StringUtils.removeEnd("www.domain.com", ".com.") = "www,domain" + * StringUtils.removeEnd("www.domain.com", ".com") = "www.domain" + * StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com" + * StringUtils.removeEnd("abc", "") = "abc" + *+ * + * @param str the source String to search, may be null + * @param remove the String to search for and remove, may be null + * @return the substring with the string removed if found, + *
null
if null String input
+ * @since 2.1
+ */
+ public static String removeEnd(String str, String remove) {
+ if (str == null || str.length() == 0 || remove == null || remove.length() == 0) {
+ return str;
+ }
+ if (str.endsWith(remove)) {
+ return str.substring(0, str.length() - remove.length());
+ }
+ return str;
+ }
+
// Replacing
//-----------------------------------------------------------------------
/**
@@ -4355,72 +4427,4 @@ private static int min(int a, int b, int c) {
return a;
}
- /**
- * Removes a substring only if it is at the begining of a source string, otherwise returns the source string. - * - *
A null
source string will return null
.
- * An empty ("") source string will return the empty string.
- * A null
search string will return the source string.
- * StringUtils.removeStart(null, *) = null - * StringUtils.removeStart("", *) = "" - * StringUtils.removeStart(*, null) = * - * StringUtils.removeStart("www.domain.com", "www.") = "domain.com" - * StringUtils.removeStart("domain.com", "www.") = "domain.com" - * StringUtils.removeStart("abc", "") = "abc" - *- * - * @param string the source String to search, may be null - * @param remove the String to search for, may be null - * @return the substring after the optional occurrence of the separator, - *
null
if null String input
- */
- public static String removeStart(String str, String remove) {
- if (str == null || str.length() == 0) {
- return str;
- }
- if (remove == null || remove.length() == 0) {
- return str;
- }
- if (str.startsWith(remove)){
- return str.substring(remove.length());
- }
- return str;
- }
-
- /**
- * Removes a substring only if it is at the end of a source string, otherwise returns the source string. - * - *
A null
source string will return null
.
- * An empty ("") source string will return the empty string.
- * A null
search string will return the source string.
- * StringUtils.removeEnd(null, *) = null - * StringUtils.removeEnd("", *) = "" - * StringUtils.removeEnd(*, null) = * - * StringUtils.removeEnd("www.domain.com", ".com.") = "www,domain" - * StringUtils.removeEnd("www.domain.com", ".com") = "www.domain" - * StringUtils.removeEnd("abc", "") = "abc" - *- * - * @param string the source String to search, may be null - * @param remove the String to search for, may be null - * @return the substring after the optional occurrence of the separator, - *
null
if null String input
- */
- public static String removeEnd(String str, String remove) {
- if (str == null || str.length() == 0) {
- return str;
- }
- if (remove == null || remove.length() == 0) {
- return str;
- }
- if (str.endsWith(remove)) {
- return str.substring(0, str.length() - remove.length());
- }
- return str;
- }
-
}
diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java
index ba9153684..ca71c1145 100644
--- a/src/test/org/apache/commons/lang/StringUtilsTest.java
+++ b/src/test/org/apache/commons/lang/StringUtilsTest.java
@@ -75,7 +75,7 @@
* @author Henning P. Schmiedehausen
* @author Phil Steitz
* @author Gary Gregory
- * @version $Id: StringUtilsTest.java,v 1.54 2003/10/29 02:16:30 ggregory Exp $
+ * @version $Id: StringUtilsTest.java,v 1.55 2003/11/01 19:20:35 scolebourne Exp $
*/
public class StringUtilsTest extends TestCase {
@@ -1000,6 +1000,7 @@ public void testRemoveStart() {
assertEquals(StringUtils.removeStart("www.domain.com", "www."), "domain.com");
assertEquals(StringUtils.removeStart("domain.com", "www."), "domain.com");
assertEquals(StringUtils.removeStart("domain.com", ""), "domain.com");
+ assertEquals(StringUtils.removeStart("domain.com", null), "domain.com");
}
public void testRemoveEnd() {
@@ -1017,6 +1018,7 @@ public void testRemoveEnd() {
assertEquals(StringUtils.removeEnd("www.domain.com", ".com"), "www.domain");
assertEquals(StringUtils.removeEnd("www.domain", ".com"), "www.domain");
assertEquals(StringUtils.removeEnd("domain.com", ""), "domain.com");
+ assertEquals(StringUtils.removeEnd("domain.com", null), "domain.com");
}
}