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:42 UTC
[commons-lang] 03/03: Add Add
org.apache.commons.lang3.StringUtils.substringAfterLast(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 062bc6fe7d48d4eb4cdf3abec09e7547a0a9aed7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 09:32:30 2020 -0400
Add Add org.apache.commons.lang3.StringUtils.substringAfterLast(String,
int).
---
src/changes/changes.xml | 3 +-
.../java/org/apache/commons/lang3/StringUtils.java | 37 ++++++++++++++++++++++
.../commons/lang3/StringUtilsSubstringTest.java | 16 ++++++++++
3 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6133923..2506d0c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -82,7 +82,8 @@ 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="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>
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 36dc7ac..6778720 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());
}
+ /**
+ * <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
//-----------------------------------------------------------------------
/**
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
index a930e03..9a1c014 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() {