Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).

This commit is contained in:
Gary Gregory 2020-06-27 09:24:06 -04:00
parent 1621a23d2b
commit d68a7a4189
3 changed files with 52 additions and 0 deletions

View File

@ -82,6 +82,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1567" type="update" dev="ggregory" due-to="Miguel Muñoz, Bruno P. Kinoshita, Gary Gregory">Fixed Javadocs for setTestRecursive() #556.</action>
<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="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

@ -8548,6 +8548,42 @@ public class StringUtils {
return str.substring(start, end);
}
/**
* <p>Gets the substring after the first 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.substringAfter(null, *) = null
* StringUtils.substringAfter("", *) = ""
* StringUtils.substringAfter("abc", 'a') = "bc"
* StringUtils.substringAfter("abcba", 'b') = "cba"
* StringUtils.substringAfter("abc", 'c') = ""
* StringUtils.substringAfter("abc", 'd') = ""
* StringUtils.substringAfter(" abc", 32) = "abc"
* </pre>
*
* @param str the String to get a substring from, may be null
* @param separator the character to search.
* @return the substring after the first occurrence of the separator,
* {@code null} if null String input
* @since 3.11
*/
public static String substringAfter(final String str, final int separator) {
if (isEmpty(str)) {
return str;
}
final int pos = str.indexOf(separator);
if (pos == INDEX_NOT_FOUND) {
return EMPTY;
}
return str.substring(pos + 1);
}
/**
* <p>Gets the substring after the first occurrence of a separator.
* The separator is not returned.</p>

View File

@ -168,6 +168,21 @@ public class StringUtilsSubstringTest {
assertEquals("", StringUtils.substringAfter("abc", "d"));
}
@Test
public void testSubstringAfter_StringInt() {
assertNull(StringUtils.substringAfter(null, 0));
assertNull(StringUtils.substringAfter(null, 'X'));
assertEquals("", StringUtils.substringAfter("", 0));
assertEquals("", StringUtils.substringAfter("", 'X'));
assertEquals("", StringUtils.substringAfter("foo", 0));
assertEquals("ot", StringUtils.substringAfter("foot", 'o'));
assertEquals("bc", StringUtils.substringAfter("abc", 'a'));
assertEquals("cba", StringUtils.substringAfter("abcba", 'b'));
assertEquals("", StringUtils.substringAfter("abc", 'c'));
assertEquals("", StringUtils.substringAfter("abc", 'd'));
}
@Test
public void testSubstringBeforeLast_StringString() {
assertEquals("fooXXbar", StringUtils.substringBeforeLast("fooXXbarXXbaz", "XX"));