Added public static String removeStart(String str, String remove).
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137697 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java
index 5410f23..d4017c3 100644
--- a/src/java/org/apache/commons/lang/StringUtils.java
+++ b/src/java/org/apache/commons/lang/StringUtils.java
@@ -145,7 +145,7 @@
* @author Gary Gregory
* @author Phil Steitz
* @since 1.0
- * @version $Id: StringUtils.java,v 1.112 2003/10/23 20:49:22 scolebourne Exp $
+ * @version $Id: StringUtils.java,v 1.113 2003/10/29 01:49:47 ggregory Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@@ -4353,4 +4353,39 @@
return a;
}
+ /**
+ * <p>Removes a substring only if it is at the begining of a source string, otherwise returns the source string.
+ *
+ * <p>A <code>null</code> source string will return <code>null</code>.
+ * An empty ("") source string will return the empty string.
+ * A <code>null</code> search string will return the source string.</p>
+ *
+ * <pre>
+ * 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"
+ * </pre>
+ *
+ * @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,
+ * <code>null</code> 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;
+ }
+ int pos = str.indexOf(remove);
+ if (pos == -1) {
+ return str;
+ }
+ return str.substring(pos + remove.length());
+ }
+
}
diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java
index 500ada7..4ef64ac 100644
--- a/src/test/org/apache/commons/lang/StringUtilsTest.java
+++ b/src/test/org/apache/commons/lang/StringUtilsTest.java
@@ -75,7 +75,7 @@
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author Phil Steitz
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
- * @version $Id: StringUtilsTest.java,v 1.52 2003/08/21 22:13:24 ggregory Exp $
+ * @version $Id: StringUtilsTest.java,v 1.53 2003/10/29 01:50:14 ggregory Exp $
*/
public class StringUtilsTest extends TestCase {
@@ -986,5 +986,22 @@
assertNotNull(StringUtils.EMPTY);
assertEquals("", StringUtils.EMPTY);
}
+
+ public void testRemoveStart() {
+ // StringUtils.removeStart("", *) = ""
+ assertNull(StringUtils.removeStart(null, null));
+ assertNull(StringUtils.removeStart(null, ""));
+ assertNull(StringUtils.removeStart(null, "a"));
+
+ // StringUtils.removeStart(*, null) = *
+ assertEquals(StringUtils.removeStart("", null), "");
+ assertEquals(StringUtils.removeStart("", ""), "");
+ assertEquals(StringUtils.removeStart("", "a"), "");
+
+ // All others:
+ assertEquals(StringUtils.removeStart("www.domain.com", "www."), "domain.com");
+ assertEquals(StringUtils.removeStart("domain.com", "www."), "domain.com");
+ assertEquals(StringUtils.removeStart("domain.com", ""), "domain.com");
+ }
}