ggregory 2003/10/28 18:16:15
Modified: lang/src/java/org/apache/commons/lang StringUtils.java
Log:
Added public static String removeEnd(String str, String remove).
Reimpl'd removeStart.
Revision Changes Path
1.114 +40 -5
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.113
retrieving revision 1.114
diff -u -r1.113 -r1.114
--- StringUtils.java 29 Oct 2003 01:49:47 -0000 1.113
+++ StringUtils.java 29 Oct 2003 02:16:15 -0000 1.114
@@ -202,6 +202,7 @@
* instance to operate.</p>
*/
public StringUtils() {
+ // no init.
}
// Empty checks
@@ -2801,7 +2802,8 @@
lastIdx--;
}
} else if (last == '\r') {
-
+ // why is this block empty?
+ // just to skip incrementing the index?
} else {
lastIdx++;
}
@@ -4381,11 +4383,44 @@
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;
+ }
+
+ /**
+ * <p>Removes a substring only if it is at the end 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.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"
+ * </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 removeEnd(String str, String remove) {
+ if (str == null || str.length() == 0) {
+ return str;
+ }
+ if (remove == null || remove.length() == 0) {
return str;
}
- return str.substring(pos + remove.length());
+ if (str.endsWith(remove)) {
+ return str.substring(0, str.length() - remove.length());
+ }
+ return str;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]