diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 61339233b..2506d0c96 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -82,7 +82,8 @@ The type attribute can be add,update,fix,remove. Fixed Javadocs for setTestRecursive() #556. ToStringBuilder.reflectionToString - Wrong JSON format when object has a List of Enum. Make org.apache.commons.lang3.CharSequenceUtils.toCharArray(CharSequence) public. - Add org.apache.commons.lang3.StringUtils.substringAfter(String, int). + Add org.apache.commons.lang3.StringUtils.substringAfter(String, int). + Add org.apache.commons.lang3.StringUtils.substringAfterLast(String, int). org.apache.commons:commons-parent 50 -> 51. org.junit-pioneer:junit-pioneer 0.5.4 -> 0.6.0. org.junit.jupiter:junit-jupiter 5.6.0 -> 5.6.1. diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 36dc7ac1d..67787200d 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -8672,6 +8672,43 @@ public class StringUtils { return str.substring(pos + separator.length()); } + /** + *

Gets the substring after the last occurrence of a separator. + * The separator is not returned.

+ * + *

A {@code null} string input will return {@code null}. + * An empty ("") string input will return the empty string. + * + *

If nothing is found, the empty string is returned.

+ * + *
+     * StringUtils.substringAfterLast(null, *)      = null
+     * StringUtils.substringAfterLast("", *)        = ""
+     * StringUtils.substringAfterLast("abc", 'a')   = "bc"
+     * StringUtils.substringAfterLast(" bc", 32)    = "bc"
+     * StringUtils.substringAfterLast("abcba", 'b') = "a"
+     * StringUtils.substringAfterLast("abc", 'c')   = ""
+     * StringUtils.substringAfterLast("a", 'a')     = ""
+     * StringUtils.substringAfterLast("a", 'z')     = ""
+     * 
+ * + * @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 occurrence of the separator, + * {@code null} if null String input + * @since 3.11 + */ + public static String substringAfterLast(final String str, final int separator) { + if (isEmpty(str)) { + return str; + } + final int pos = str.lastIndexOf(separator); + if (pos == INDEX_NOT_FOUND || pos == str.length() - 1) { + return EMPTY; + } + return str.substring(pos + 1); + } + // SubStringAfter/SubStringBefore //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java index a930e0384..9a1c014fe 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java @@ -228,6 +228,22 @@ public class StringUtilsSubstringTest { assertEquals("", StringUtils.substringAfterLast("abc", "")); } + @Test + public void testSubstringAfterLast_StringInt() { + assertNull(StringUtils.substringAfterLast(null, 0)); + assertNull(StringUtils.substringAfterLast(null, 'X')); + assertEquals("", StringUtils.substringAfterLast("", 0)); + assertEquals("", StringUtils.substringAfterLast("", 'a')); + + assertEquals("", StringUtils.substringAfterLast("foo", 0)); + 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')); + } + //----------------------------------------------------------------------- @Test public void testSubstringBetween_StringString() {