From d68a7a4189be8032045f952952c1aa9c764a3b9a Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 27 Jun 2020 09:24:06 -0400 Subject: [PATCH] Add org.apache.commons.lang3.StringUtils.substringAfter(String, int). --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/StringUtils.java | 36 +++++++++++++++++++ .../lang3/StringUtilsSubstringTest.java | 15 ++++++++ 3 files changed, 52 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5c8b3e159..61339233b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -82,6 +82,7 @@ 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). 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 5b8723016..36dc7ac1d 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -8548,6 +8548,42 @@ public class StringUtils { return str.substring(start, end); } + /** + *

Gets the substring after the first 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.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"
+     * 
+ * + * @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); + } + /** *

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

diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java index 76bd28290..a930e0384 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java @@ -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"));