Add Add org.apache.commons.lang3.StringUtils.substringAfterLast(String,

int).
This commit is contained in:
Gary Gregory 2020-06-27 09:32:30 -04:00
parent 75fa90e96c
commit 062bc6fe7d
3 changed files with 55 additions and 1 deletions

View File

@ -83,6 +83,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1542" type="update" dev="ggregory" due-to=" Trần Ngọc Khoa, Gary Gregory">ToStringBuilder.reflectionToString - Wrong JSON format when object has a List of Enum.</action>
<action type="update" dev="ggregory">Make org.apache.commons.lang3.CharSequenceUtils.toCharArray(CharSequence) public.</action>
<action type="add" dev="ggregory">Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).</action>
<action type="add" dev="ggregory">Add org.apache.commons.lang3.StringUtils.substringAfterLast(String, int).</action>
<action type="update" dev="ggregory">org.apache.commons:commons-parent 50 -> 51.</action>
<action type="update" dev="ggregory">org.junit-pioneer:junit-pioneer 0.5.4 -> 0.6.0.</action>
<action type="update" dev="ggregory">org.junit.jupiter:junit-jupiter 5.6.0 -> 5.6.1.</action>

View File

@ -8672,6 +8672,43 @@ public static String substringAfterLast(final String str, final String separator
return str.substring(pos + separator.length());
}
/**
* <p>Gets the substring after the last occurrence of a separator.
* The separator is not returned.</p>
*
* <p>A {@code null} string input will return {@code null}.
* An empty ("") string input will return the empty string.
*
* <p>If nothing is found, the empty string is returned.</p>
*
* <pre>
* 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') = ""
* </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 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
//-----------------------------------------------------------------------
/**

View File

@ -228,6 +228,22 @@ public void testSubstringAfterLast_StringString() {
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() {