scolebourne 2003/11/01 11:20:35 Modified: lang/src/test/org/apache/commons/lang StringUtilsTest.java lang/src/java/org/apache/commons/lang StringUtils.java Log: Move remove code to relevant position in source file Revision Changes Path 1.55 +3 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java Index: StringUtilsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v retrieving revision 1.54 retrieving revision 1.55 diff -u -r1.54 -r1.55 --- StringUtilsTest.java 29 Oct 2003 02:16:30 -0000 1.54 +++ StringUtilsTest.java 1 Nov 2003 19:20:35 -0000 1.55 @@ -1000,6 +1000,7 @@ 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 @@ 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"); } } 1.115 +74 -70 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.114 retrieving revision 1.115 diff -u -r1.114 -r1.115 --- StringUtils.java 29 Oct 2003 02:16:15 -0000 1.114 +++ StringUtils.java 1 Nov 2003 19:20:35 -0000 1.115 @@ -80,7 +80,9 @@ * - substring extraction relative to other strings</li> * <li><b>Split/Join</b> * - splits a String into an array of substrings and vice versa</li> - * <li><b>Replace/Delete/Overlay</b> + * <li><b>Remove/Delete</b> + * - removes part of a String</li> + * <li><b>Replace/Overlay</b> * - Searches a String and replaces one String with another</li> * <li><b>Chomp/Chop</b> * - removes the last part of a String</li> @@ -2457,6 +2459,76 @@ return new String(chs, 0, count); } + // Remove + //----------------------------------------------------------------------- + /** + * <p>Removes a substring only if it is at the begining of a source string, + * otherwise returns the source string.</p> + * + * <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("www.domain.com", "domain") = "www.domain.com" + * StringUtils.removeStart("abc", "") = "abc" + * </pre> + * + * @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, + * <code>null</code> 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; + } + + /** + * <p>Removes a substring only if it is at the end of a source string, + * otherwise returns the source string.</p> + * + * <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("www.domain.com", "domain") = "www.domain.com" + * StringUtils.removeEnd("abc", "") = "abc" + * </pre> + * + * @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, + * <code>null</code> 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 //----------------------------------------------------------------------- /** @@ -4353,74 +4425,6 @@ a = c; } 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; - } - 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; - } - 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]