From 576920fa5bb22de1bfcfd12e80891bd3f0970d04 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Wed, 29 Oct 2003 02:16:15 +0000 Subject: [PATCH] Added public static String removeEnd(String str, String remove). Reimpl'd removeStart. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137698 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/StringUtils.java | 45 ++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index d4017c383..08e29d904 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -145,7 +145,7 @@ import java.util.List; * @author Gary Gregory * @author Phil Steitz * @since 1.0 - * @version $Id: StringUtils.java,v 1.113 2003/10/29 01:49:47 ggregory Exp $ + * @version $Id: StringUtils.java,v 1.114 2003/10/29 02:16:15 ggregory Exp $ */ public class StringUtils { // Performance testing notes (JDK 1.4, Jul03, scolebourne) @@ -202,6 +202,7 @@ public class StringUtils { * instance to operate.

*/ public StringUtils() { + // no init. } // Empty checks @@ -2801,7 +2802,8 @@ public class StringUtils { lastIdx--; } } else if (last == '\r') { - + // why is this block empty? + // just to skip incrementing the index? } else { lastIdx++; } @@ -4381,11 +4383,44 @@ public class StringUtils { if (remove == null || remove.length() == 0) { return str; } - int pos = str.indexOf(remove); - if (pos == -1) { + 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; } - return str.substring(pos + remove.length()); + if (remove == null || remove.length() == 0) { + return str; + } + if (str.endsWith(remove)) { + return str.substring(0, str.length() - remove.length()); + } + return str; } }