You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/06/27 13:32:40 UTC
[commons-lang] 01/03: Add
org.apache.commons.lang3.StringUtils.substringAfter(String, int).
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit d68a7a4189be8032045f952952c1aa9c764a3b9a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 09:24:06 2020 -0400
Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/lang3/StringUtils.java | 36 ++++++++++++++++++++++
.../commons/lang3/StringUtilsSubstringTest.java | 15 +++++++++
3 files changed, 52 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5c8b3e1..6133923 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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>
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 5b87230..36dc7ac 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -8554,6 +8554,42 @@ public class StringUtils {
*
* <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>
+ *
+ * <p>A {@code null} string input will return {@code null}.
+ * An empty ("") string input will return the empty string.
* A {@code null} separator will return the empty string if the
* input string is not {@code null}.</p>
*
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
index 76bd282..a930e03 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
@@ -169,6 +169,21 @@ public class StringUtilsSubstringTest {
}
@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"));