This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 8e0dd9df1 fix substringAfter(Last) for supplementary code points 
(#1707)
8e0dd9df1 is described below

commit 8e0dd9df142dd25fdb0de1d7b34937980c08c03f
Author: alhuda <[email protected]>
AuthorDate: Mon Jun 15 17:34:24 2026 +0530

    fix substringAfter(Last) for supplementary code points (#1707)
---
 src/main/java/org/apache/commons/lang3/StringUtils.java     |  6 +++---
 .../org/apache/commons/lang3/StringUtilsSubstringTest.java  | 13 +++++++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index eb9115dd1..db9b225e6 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -8260,7 +8260,7 @@ public static String substringAfter(final String str, 
final int find) {
         if (pos == INDEX_NOT_FOUND) {
             return EMPTY;
         }
-        return str.substring(pos + 1);
+        return str.substring(pos + Character.charCount(find));
     }
 
     /**
@@ -8337,10 +8337,10 @@ public static String substringAfterLast(final String 
str, final int find) {
             return str;
         }
         final int pos = str.lastIndexOf(find);
-        if (pos == INDEX_NOT_FOUND || pos == str.length() - 1) {
+        if (pos == INDEX_NOT_FOUND || pos == str.length() - 
Character.charCount(find)) {
             return EMPTY;
         }
-        return str.substring(pos + 1);
+        return str.substring(pos + Character.charCount(find));
     }
 
     /**
diff --git 
a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
index 203327a20..9c7d18620 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
@@ -175,6 +175,12 @@ void testSubstringAfter_StringInt() {
         assertEquals("cba", StringUtils.substringAfter("abcba", 'b'));
         assertEquals("", StringUtils.substringAfter("abc", 'c'));
         assertEquals("", StringUtils.substringAfter("abc", 'd'));
+
+        // a supplementary code point occupies two chars
+        final int grin = 0x1F600;
+        final String s = new String(Character.toChars(grin));
+        assertEquals("world", StringUtils.substringAfter("hello" + s + 
"world", grin));
+        assertEquals("", StringUtils.substringAfter("hello" + s, grin));
     }
 
     @Test
@@ -211,6 +217,13 @@ void testSubstringAfterLast_StringInt() {
         assertEquals("a", StringUtils.substringAfterLast("abcba", 'b'));
         assertEquals("", StringUtils.substringAfterLast("abc", 'c'));
         assertEquals("", StringUtils.substringAfterLast("", 'd'));
+
+        // a supplementary code point occupies two chars
+        final int grin = 0x1F600;
+        final String s = new String(Character.toChars(grin));
+        assertEquals("", StringUtils.substringAfterLast("hello" + s, grin));
+        assertEquals("x", StringUtils.substringAfterLast(s + "a" + s + "x", 
grin));
+        assertEquals("world", StringUtils.substringAfterLast("a" + s + 
"world", grin));
     }
 
     @Test

Reply via email to