scolebourne 2003/07/25 15:22:31 Modified: lang/src/test/org/apache/commons/lang StringUtilsTest.java StringUtilsSubstringTest.java lang/src/java/org/apache/commons/lang StringUtils.java Log: Rename slice functions to substringAfter/substringBefore Revision Changes Path 1.39 +1 -104 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.38 retrieving revision 1.39 diff -u -r1.38 -r1.39 --- StringUtilsTest.java 25 Jul 2003 00:50:00 -0000 1.38 +++ StringUtilsTest.java 25 Jul 2003 22:22:30 -0000 1.39 @@ -573,109 +573,6 @@ } } - public void testSliceFunctions() { - - String[][] sliceCases = { - {"foo\n", "foo"}, - {"foo\nbar", "foo"}, - {"foo\nbar\n", "foo\nbar"}, - {"foo\nbar\nbaz", "foo\nbar"}, - {null, null}, - {"", ""}, - {"\n", ""}, - {"abc \n", "abc "}, - {"abc\r\n", "abc\r"}, - {"foo", "foo"}, - }; - for (int i = 0; i < sliceCases.length; i++) { - String original = sliceCases[i][0]; - String expectedResult = sliceCases[i][1]; - assertEquals("slice(String) failed", - expectedResult, StringUtils.slice(original)); - } - } - - public void testSlice_StringString() { - assertEquals("fooXXbar", StringUtils.slice("fooXXbarXXbaz", "XX")); - - assertEquals(null, StringUtils.slice(null, null)); - assertEquals(null, StringUtils.slice(null, "")); - assertEquals(null, StringUtils.slice(null, "XX")); - assertEquals("", StringUtils.slice("", null)); - assertEquals("", StringUtils.slice("", "")); - assertEquals("", StringUtils.slice("", "XX")); - - assertEquals("foo", StringUtils.slice("foo", null)); - assertEquals("foo", StringUtils.slice("foo", "b")); - assertEquals("fo", StringUtils.slice("foo", "o")); - assertEquals("abc\r\n", StringUtils.slice("abc\r\n", "d")); - assertEquals("abc", StringUtils.slice("abcdabc", "d")); - assertEquals("abcdabc", StringUtils.slice("abcdabcd", "d")); - assertEquals("a", StringUtils.slice("abc", "b")); - assertEquals("abc ", StringUtils.slice("abc \n", "\n")); - assertEquals("a", StringUtils.slice("a", null)); - assertEquals("a", StringUtils.slice("a", "")); - assertEquals("", StringUtils.slice("a", "a")); - } - - public void testSliceRemainder_StringString() { - assertEquals("baz", StringUtils.sliceRemainder("fooXXbarXXbaz", "XX")); - - assertEquals(null, StringUtils.sliceRemainder(null, null)); - assertEquals(null, StringUtils.sliceRemainder(null, "")); - assertEquals(null, StringUtils.sliceRemainder(null, "XX")); - assertEquals("", StringUtils.sliceRemainder("", null)); - assertEquals("", StringUtils.sliceRemainder("", "")); - assertEquals("", StringUtils.sliceRemainder("", "a")); - - assertEquals("", StringUtils.sliceRemainder("foo", null)); - assertEquals("", StringUtils.sliceRemainder("foo", "b")); - assertEquals("t", StringUtils.sliceRemainder("foot", "o")); - assertEquals("bc", StringUtils.sliceRemainder("abc", "a")); - assertEquals("a", StringUtils.sliceRemainder("abcba", "b")); - assertEquals("", StringUtils.sliceRemainder("abc", "c")); - assertEquals("", StringUtils.sliceRemainder("", "d")); - assertEquals("", StringUtils.sliceRemainder("abc", "")); - } - - public void testSliceFirst_StringString() { - assertEquals("foo", StringUtils.sliceFirst("fooXXbarXXbaz", "XX")); - - assertEquals(null, StringUtils.sliceFirst(null, null)); - assertEquals(null, StringUtils.sliceFirst(null, "")); - assertEquals(null, StringUtils.sliceFirst(null, "XX")); - assertEquals("", StringUtils.sliceFirst("", null)); - assertEquals("", StringUtils.sliceFirst("", "")); - assertEquals("", StringUtils.sliceFirst("", "XX")); - - assertEquals("foo", StringUtils.sliceFirst("foo", null)); - assertEquals("foo", StringUtils.sliceFirst("foo", "b")); - assertEquals("f", StringUtils.sliceFirst("foot", "o")); - assertEquals("", StringUtils.sliceFirst("abc", "a")); - assertEquals("a", StringUtils.sliceFirst("abcba", "b")); - assertEquals("ab", StringUtils.sliceFirst("abc", "c")); - assertEquals("", StringUtils.sliceFirst("abc", "")); - } - - public void testSliceFirstRemainder_StringString() { - assertEquals("barXXbaz", StringUtils.sliceFirstRemainder("fooXXbarXXbaz", "XX")); - - assertEquals(null, StringUtils.sliceFirstRemainder(null, null)); - assertEquals(null, StringUtils.sliceFirstRemainder(null, "")); - assertEquals(null, StringUtils.sliceFirstRemainder(null, "XX")); - assertEquals("", StringUtils.sliceFirstRemainder("", null)); - assertEquals("", StringUtils.sliceFirstRemainder("", "")); - assertEquals("", StringUtils.sliceFirstRemainder("", "XX")); - - assertEquals("", StringUtils.sliceFirstRemainder("foo", null)); - assertEquals("ot", StringUtils.sliceFirstRemainder("foot", "o")); - assertEquals("bc", StringUtils.sliceFirstRemainder("abc", "a")); - assertEquals("cba", StringUtils.sliceFirstRemainder("abcba", "b")); - assertEquals("", StringUtils.sliceFirstRemainder("abc", "c")); - assertEquals("abc", StringUtils.sliceFirstRemainder("abc", "")); - assertEquals("", StringUtils.sliceFirstRemainder("abc", "d")); - } - //----------------------------------------------------------------------- public void testRightPad_StringInt() { assertEquals(null, StringUtils.rightPad(null, 5)); 1.9 +84 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java Index: StringUtilsSubstringTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- StringUtilsSubstringTest.java 21 Jul 2003 00:41:13 -0000 1.8 +++ StringUtilsSubstringTest.java 25 Jul 2003 22:22:30 -0000 1.9 @@ -178,6 +178,89 @@ } catch (IllegalArgumentException ex) {} } + //----------------------------------------------------------------------- + public void testSubstringBefore_StringString() { + assertEquals("foo", StringUtils.substringBefore("fooXXbarXXbaz", "XX")); + + assertEquals(null, StringUtils.substringBefore(null, null)); + assertEquals(null, StringUtils.substringBefore(null, "")); + assertEquals(null, StringUtils.substringBefore(null, "XX")); + assertEquals("", StringUtils.substringBefore("", null)); + assertEquals("", StringUtils.substringBefore("", "")); + assertEquals("", StringUtils.substringBefore("", "XX")); + + assertEquals("foo", StringUtils.substringBefore("foo", null)); + assertEquals("foo", StringUtils.substringBefore("foo", "b")); + assertEquals("f", StringUtils.substringBefore("foot", "o")); + assertEquals("", StringUtils.substringBefore("abc", "a")); + assertEquals("a", StringUtils.substringBefore("abcba", "b")); + assertEquals("ab", StringUtils.substringBefore("abc", "c")); + assertEquals("", StringUtils.substringBefore("abc", "")); + } + + public void testSubstringAfter_StringString() { + assertEquals("barXXbaz", StringUtils.substringAfter("fooXXbarXXbaz", "XX")); + + assertEquals(null, StringUtils.substringAfter(null, null)); + assertEquals(null, StringUtils.substringAfter(null, "")); + assertEquals(null, StringUtils.substringAfter(null, "XX")); + assertEquals("", StringUtils.substringAfter("", null)); + assertEquals("", StringUtils.substringAfter("", "")); + assertEquals("", StringUtils.substringAfter("", "XX")); + + assertEquals("", StringUtils.substringAfter("foo", null)); + assertEquals("ot", StringUtils.substringAfter("foot", "o")); + assertEquals("bc", StringUtils.substringAfter("abc", "a")); + assertEquals("cba", StringUtils.substringAfter("abcba", "b")); + assertEquals("", StringUtils.substringAfter("abc", "c")); + assertEquals("abc", StringUtils.substringAfter("abc", "")); + assertEquals("", StringUtils.substringAfter("abc", "d")); + } + + public void testSubstringBeforeLast_StringString() { + assertEquals("fooXXbar", StringUtils.substringBeforeLast("fooXXbarXXbaz", "XX")); + + assertEquals(null, StringUtils.substringBeforeLast(null, null)); + assertEquals(null, StringUtils.substringBeforeLast(null, "")); + assertEquals(null, StringUtils.substringBeforeLast(null, "XX")); + assertEquals("", StringUtils.substringBeforeLast("", null)); + assertEquals("", StringUtils.substringBeforeLast("", "")); + assertEquals("", StringUtils.substringBeforeLast("", "XX")); + + assertEquals("foo", StringUtils.substringBeforeLast("foo", null)); + assertEquals("foo", StringUtils.substringBeforeLast("foo", "b")); + assertEquals("fo", StringUtils.substringBeforeLast("foo", "o")); + assertEquals("abc\r\n", StringUtils.substringBeforeLast("abc\r\n", "d")); + assertEquals("abc", StringUtils.substringBeforeLast("abcdabc", "d")); + assertEquals("abcdabc", StringUtils.substringBeforeLast("abcdabcd", "d")); + assertEquals("a", StringUtils.substringBeforeLast("abc", "b")); + assertEquals("abc ", StringUtils.substringBeforeLast("abc \n", "\n")); + assertEquals("a", StringUtils.substringBeforeLast("a", null)); + assertEquals("a", StringUtils.substringBeforeLast("a", "")); + assertEquals("", StringUtils.substringBeforeLast("a", "a")); + } + + public void testSubstringAfterLast_StringString() { + assertEquals("baz", StringUtils.substringAfterLast("fooXXbarXXbaz", "XX")); + + assertEquals(null, StringUtils.substringAfterLast(null, null)); + assertEquals(null, StringUtils.substringAfterLast(null, "")); + assertEquals(null, StringUtils.substringAfterLast(null, "XX")); + assertEquals("", StringUtils.substringAfterLast("", null)); + assertEquals("", StringUtils.substringAfterLast("", "")); + assertEquals("", StringUtils.substringAfterLast("", "a")); + + assertEquals("", StringUtils.substringAfterLast("foo", null)); + assertEquals("", StringUtils.substringAfterLast("foo", "b")); + assertEquals("t", StringUtils.substringAfterLast("foot", "o")); + assertEquals("bc", StringUtils.substringAfterLast("abc", "a")); + assertEquals("a", StringUtils.substringAfterLast("abcba", "b")); + assertEquals("", StringUtils.substringAfterLast("abc", "c")); + assertEquals("", StringUtils.substringAfterLast("", "d")); + assertEquals("", StringUtils.substringAfterLast("abc", "")); + } + + //----------------------------------------------------------------------- public void testCountMatches_String() { assertEquals(0, StringUtils.countMatches(null, null)); assertEquals(0, StringUtils.countMatches("blah", null)); 1.79 +167 -201 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.78 retrieving revision 1.79 diff -u -r1.78 -r1.79 --- StringUtils.java 25 Jul 2003 00:50:00 -0000 1.78 +++ StringUtils.java 25 Jul 2003 22:22:30 -0000 1.79 @@ -74,7 +74,7 @@ * - index of any of a set of Strings * <li><b>ContainsOnly/ContainsNone</b> * - does String contain only/none of these characters - * <li><b>SubString/Left/Right/Mid</b> + * <li><b>SubString/Left/Right/Mid/SubStringBefore/SubStringAfter</b> * - null-safe substring extraction * <li><b>Split</b> * - splits a String into an array of subtrings based on a separator @@ -82,8 +82,8 @@ * - joins an array of Strings into one with optional separator * <li><b>Replace/Delete/Overlay</b> * - Searches a String and replaces one String with another - * <li><b>Chomp/Chop/Slice</b> - * - searches a String and returns the substring before/after the separator + * <li><b>Chomp/Chop</b> + * - removes the last part of a String * <li><b>LeftPad/RightPad/Center/Repeat</b> * - pads a String * <li><b>UpperCase/LowerCase/SwapCase/Capitalise/Uncapitalise</b> @@ -1547,6 +1547,161 @@ } } + // SubStringAfter/SubStringBefore + //----------------------------------------------------------------------- + + /** + * <p>Gets the substring before the first occurance of a separator. + * The separator is not returned.</p> + * + * <p>A <code>null</code> string input will return <code>null</code>. + * An empty ("") string input will return the empty string. + * A <code>null</code> separator will return the input string.</p> + * + * <pre> + * StringUtils.substringBefore(null, *) = null + * StringUtils.substringBefore("", *) = "" + * StringUtils.substringBefore("abc", "a") = "" + * StringUtils.substringBefore("abcba", "b") = "a" + * StringUtils.substringBefore("abc", "c") = "ab" + * StringUtils.substringBefore("abc", "d") = "abc" + * StringUtils.substringBefore("abc", "") = "" + * StringUtils.substringBefore("abc", null) = "abc" + * </pre> + * + * @param str the String to get a substring from, may be null + * @param separator the String to search for, may be null + * @return the substring before the first occurance of the separator, + * <code>null</code> if null String input + */ + public static String substringBefore(String str, String separator) { + if (str == null || separator == null || str.length() == 0) { + return str; + } + if (separator.length() == 0) { + return ""; + } + int pos = str.indexOf(separator); + if (pos == -1) { + return str; + } + return str.substring(0, pos); + } + + /** + * <p>Gets the substring after the first occurance of a separator. + * The separator is not returned.</p> + * + * <p>A <code>null</code> string input will return <code>null</code>. + * An empty ("") string input will return the empty string. + * A <code>null</code> separator will return the empty string if the + * input string is not <code>null</code>.</p> + * + * <pre> + * StringUtils.substringAfter(null, *) = null + * StringUtils.substringAfter("", *) = "" + * StringUtils.substringAfter(*, null) = "" + * StringUtils.substringAfter("abc", "a") = "bc" + * StringUtils.substringAfter("abcba", "b") = "cba" + * StringUtils.substringAfter("abc", "c") = "" + * StringUtils.substringAfter("abc", "d") = "" + * StringUtils.substringAfter("abc", "") = "abc" + * </pre> + * + * @param str the String to get a substring from, may be null + * @param separator the String to search for, may be null + * @return the substring after the first occurance of the separator, + * <code>null</code> if null String input + */ + public static String substringAfter(String str, String separator) { + if (str == null || str.length() == 0) { + return str; + } + if (separator == null) { + return ""; + } + int pos = str.indexOf(separator); + if (pos == -1) { + return ""; + } + return str.substring(pos + separator.length()); + } + + /** + * <p>Gets the substring before the last occurance of a separator. + * The separator is not returned.</p> + * + * <p>A <code>null</code> string input will return <code>null</code>. + * An empty ("") string input will return the empty string. + * An empty or <code>null</code> separator will return the input string.</p> + * + * <pre> + * StringUtils.substringBeforeLast(null, *) = null + * StringUtils.substringBeforeLast("", *) = "" + * StringUtils.substringBeforeLast("abcba", "b") = "abc" + * StringUtils.substringBeforeLast("abc", "c") = "ab" + * StringUtils.substringBeforeLast("a", "a") = "" + * StringUtils.substringBeforeLast("a", "z") = "a" + * StringUtils.substringBeforeLast("a", null) = "a" + * StringUtils.substringBeforeLast("a", "") = "a" + * </pre> + * + * @param str the String to get a substring from, may be null + * @param separator the String to search for, may be null + * @return the substring before the last occurance of the separator, + * <code>null</code> if null String input + */ + public static String substringBeforeLast(String str, String separator) { + if (str == null || separator == null || str.length() == 0 || separator.length() == 0) { + return str; + } + int pos = str.lastIndexOf(separator); + if (pos == -1) { + return str; + } + return str.substring(0, pos); + } + + /** + * <p>Gets the substring after the last occurance of a separator. + * The separator is not returned.</p> + * + * <p>A <code>null</code> string input will return <code>null</code>. + * An empty ("") string input will return the empty string. + * An empty or <code>null</code> separator will return the empty string if + * the input string is not <code>null</code>.</p> + * + * <pre> + * StringUtils.substringAfterLast(null, *) = null + * StringUtils.substringAfterLast("", *) = "" + * StringUtils.substringAfterLast(*, "") = "" + * StringUtils.substringAfterLast(*, null) = "" + * StringUtils.substringAfterLast("abc", "a") = "bc" + * StringUtils.substringAfterLast("abcba", "b") = "a" + * StringUtils.substringAfterLast("abc", "c") = "" + * StringUtils.substringAfterLast("a", "a") = "" + * StringUtils.substringAfterLast("a", "z") = "" + * </pre> + * + * @param str the String to get a substring from, may be null + * @param separator the String to search for, may be null + * @return the substring after the last occurance of the separator, + * <code>null</code> if null String input + */ + public static String substringAfterLast(String str, String separator) { + if (str == null || str.length() == 0) { + return str; + } + if (separator == null || separator.length() == 0) { + return ""; + } + int pos = str.lastIndexOf(separator); + if (pos == -1 || pos == (str.length() - separator.length())) { + return ""; + } + return str.substring(pos + separator.length()); + } + // Splitting //----------------------------------------------------------------------- @@ -2145,8 +2300,7 @@ * "<code>\r</code>", or "<code>\r\n</code>".</p> * * <p>NOTE: This method changed in 2.0. - * It now more closely matches Perl chomp. - * For the previous behavior, use [EMAIL PROTECTED] #slice(String)}.</p> + * It now more closely matches Perl chomp.</p> * * <pre> * StringUtils.chomp(null) = null @@ -2200,7 +2354,7 @@ * * <p>NOTE: This method changed in version 2.0. * It now more closely matches Perl chomp. - * For the previous behavior, use [EMAIL PROTECTED] #slice(String,String)}. + * For the previous behavior, use [EMAIL PROTECTED] #substringBeforeLast(String, String)}. * This method uses [EMAIL PROTECTED] String#endsWith(String)}.</p> * * <pre> @@ -2268,14 +2422,14 @@ /** * <p>Remove everything and return the last value of a supplied String, and - * everything after it from a String. - * [That makes no sense. Just use sliceRemainder() :-)]</p> + * everything after it from a String.</p> * * @param str the String to chomp from, must not be null * @param sep the String to chomp, must not be null * @return String chomped * @throws NullPointerException if str or sep is <code>null</code> - * @deprecated Use [EMAIL PROTECTED] #sliceRemainder(String,String)} instead. + * @deprecated Use [EMAIL PROTECTED] #substringAfterLast(String, String)} instead + * (although this doesn't include the separator) * Method will be removed in Commons Lang 3.0. */ public static String getChomp(String str, String sep) { @@ -2297,7 +2451,7 @@ * @param sep the String to chomp, must not be null * @return String without chomped beginning * @throws NullPointerException if str or sep is <code>null</code> - * @deprecated Use [EMAIL PROTECTED] #sliceFirstRemainder(String,String)} instead. + * @deprecated Use [EMAIL PROTECTED] #substringAfter(String,String)} instead. * Method will be removed in Commons Lang 3.0. */ public static String prechomp(String str, String sep) { @@ -2317,7 +2471,8 @@ * @param sep the String to chomp, must not be null * @return String prechomped * @throws NullPointerException if str or sep is <code>null</code> - * @deprecated Use [EMAIL PROTECTED] #sliceFirst(String,String)} instead. + * @deprecated Use [EMAIL PROTECTED] #substringBefore(String,String)} instead + * (although this doesn't include the separator) * Method will be removed in Commons Lang 3.0. */ public static String getPrechomp(String str, String sep) { @@ -2400,195 +2555,6 @@ return str.substring(0, lastIdx); } - - // Slicing - //----------------------------------------------------------------------- - - /** - * <p>Removes the last newline, and everything after it from a String.</p> - * - * <p>A <code>null</code> string input will return <code>null</code>. - * An empty ("") string input will return the empty string.</p> - * - * <pre> - * StringUtils.slice(null) = null - * StringUtils.slice("") = "" - * StringUtils.slice("abc \n") = "abc " - * StringUtils.slice("abc\n") = "abc" - * StringUtils.slice("abc\r\n") = "abc\r" - * StringUtils.slice("abc") = "abc" - * StringUtils.slice("abc\nabc") = "abc" - * StringUtils.slice("abc\nabc\n") = "abc\nabc" - * StringUtils.slice("\n") = "" - * </pre> - * - * <p><em>(This method was formerly named chomp or chopNewline.)</em></p> - * - * @param str the String to slice the newline from, may be null - * @return String without sliced newline, <code>null</code> if null String input - */ - public static String slice(String str) { - return slice(str, "\n"); - } - - /** - * <p>Finds the last occurence of a separator String, - * returning everything before it. The separator is not returned.</p> - * - * <p>A <code>null</code> string input will return <code>null</code>. - * An empty ("") string input will return the empty string. - * An empty or <code>null</code> separator will return the input string.</p> - * - * <pre> - * StringUtils.slice(null, *) = null - * StringUtils.slice("", *) = "" - * StringUtils.slice("abcba", "b") = "abc" - * StringUtils.slice("abc", "c") = "ab" - * StringUtils.slice("a", "a") = "" - * StringUtils.slice("a", "z") = "a" - * StringUtils.slice("a", null) = "a" - * StringUtils.slice("a", "") = "a" - * </pre> - * - * <p><em>(This method was formerly named chomp.)</em></p> - * - * @param str the String to slice from, may be null - * @param separator the String to slice, may be null - * @return String without sliced ending, <code>null</code> if null String input - */ - public static String slice(String str, String separator) { - if (str == null || separator == null || str.length() == 0 || separator.length() == 0) { - return str; - } - int pos = str.lastIndexOf(separator); - if (pos == -1) { - return str; - } - return str.substring(0, pos); - } - - /** - * <p>Finds the last occurence of a separator String, - * returning everything after it.</p> - * - * <p>A <code>null</code> string input will return <code>null</code>. - * An empty ("") string input will return the empty string. - * An empty or <code>null</code> separator will return the empty string if - * the input string is not <code>null</code>.</p> - * - * <pre> - * StringUtils.sliceRemainder(null, *) = null - * StringUtils.sliceRemainder("", *) = "" - * StringUtils.sliceRemainder(*, "") = "" - * StringUtils.sliceRemainder(*, null) = "" - * StringUtils.sliceRemainder("abc", "a") = "bc" - * StringUtils.sliceRemainder("abcba", "b") = "a" - * StringUtils.sliceRemainder("abc", "c") = "" - * StringUtils.sliceRemainder("a", "a") = "" - * StringUtils.sliceRemainder("a", "z") = "" - * </pre> - * - * <p><em>(This method was formerly named getchomp. Also, now it does not - * include the separator in the return value.)</em></p> - * - * @param str the String to slice from, may be null - * @param separator the String to slice, may be null - * @return String sliced, <code>null</code> if null String input - */ - public static String sliceRemainder(String str, String separator) { - if (str == null || str.length() == 0) { - return str; - } - if (separator == null || separator.length() == 0) { - return ""; - } - int pos = str.lastIndexOf(separator); - if (pos == -1 || pos == (str.length() - separator.length())) { - return ""; - } - return str.substring(pos + separator.length()); - } - - /** - * <p>Finds the first occurence of a separator String, - * returning everything before it. The separator is not returned.</p> - * - * <p>A <code>null</code> string input will return <code>null</code>. - * An empty ("") string input will return the empty string. - * A <code>null</code> separator will return the input string.</p> - * - * <pre> - * StringUtils.sliceFirst(null, *) = null - * StringUtils.sliceFirst("", *) = "" - * StringUtils.sliceFirst("abc", "a") = "" - * StringUtils.sliceFirst("abcba", "b") = "a" - * StringUtils.sliceFirst("abc", "c") = "ab" - * StringUtils.sliceFirst("abc", "d") = "abc" - * StringUtils.sliceFirst("abc", "") = "" - * StringUtils.sliceFirst("abc", null) = "abc" - * </pre> - * - * <p><em>(This method was formerly named getPrechomp. Also, it used to - * include the separator, but now it does not.)</em></p> - * - * @param str the String to slice from, may be null - * @param separator the String to slice, may be null - * @return sliced String, <code>null</code> if null String input - */ - public static String sliceFirst(String str, String separator) { - if (str == null || separator == null || str.length() == 0) { - return str; - } - if (separator.length() == 0) { - return ""; - } - int pos = str.indexOf(separator); - if (pos == -1) { - return str; - } - return str.substring(0, pos); - } - - /** - * <p>Finds the first occurence of a separator String, - * returning everything after it.</p> - * - * <p>A <code>null</code> string input will return <code>null</code>. - * An empty ("") string input will return the empty string. - * A <code>null</code> separator will return the empty string if the - * input string is not <code>null</code>.</p> - * - * <pre> - * StringUtils.sliceFirstRemainder(null, *) = null - * StringUtils.sliceFirstRemainder("", *) = "" - * StringUtils.sliceFirstRemainder(*, null) = "" - * StringUtils.sliceFirstRemainder("abc", "a") = "bc" - * StringUtils.sliceFirstRemainder("abcba", "b") = "cba" - * StringUtils.sliceFirstRemainder("abc", "c") = "" - * StringUtils.sliceFirstRemainder("abc", "d") = "" - * StringUtils.sliceFirstRemainder("abc", "") = "abc" - * </pre> - * - * <p><em>(This method was formerly named prechomp. Also, previously - * it included the separator in the return value; now it does not.)</em></p> - * - * @param str the String to slice from, may be null - * @param separator the String to slice, may be null - * @return String without sliced beginning, <code>null</code> if null String input - */ - public static String sliceFirstRemainder(String str, String separator) { - if (str == null || str.length() == 0) { - return str; - } - if (separator == null) { - return ""; - } - int pos = str.indexOf(separator); - if (pos == -1) { - return ""; - } - return str.substring(pos + separator.length()); - } // Conversion //-----------------------------------------------------------------------
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]