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


The following commit(s) were added to refs/heads/master by this push:
     new 4f85c16  Add StringUtils.substringBefore(String, int).
4f85c16 is described below

commit 4f85c164a1a4eeb8813b61cf46132fb91971b323
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Feb 10 17:04:11 2021 -0500

    Add StringUtils.substringBefore(String, int).
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/lang3/StringUtils.java | 38 ++++++++++++++++++++++
 .../commons/lang3/StringUtilsSubstringTest.java    | 20 +++++++++++-
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4908376..ee60ed4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -87,6 +87,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add FailableShortSupplier, handy for JDBC APIs.</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add JavaVersion.JAVA_17.</action>
     <action issue="LANG-1636" type="add" dev="ggregory" due-to="">Add missing 
boolean[] join method #686.</action>
+    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add StringUtils.substringBefore(String, int).</action>
     <!--  UPDATES -->
     <action                   type="update" dev="ggregory" due-to="Gary 
Gregory">Enable Dependabot #587.</action>
     <action                   type="update" dev="chtompki">Bump junit-jupiter 
from 5.6.2 to 5.7.0.</action>
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 478139e..b5b233d 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -8703,6 +8703,44 @@ public class StringUtils {
     }
 
     /**
+     * <p>
+     * Gets the substring before 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>
+     *
+     * <p>
+     * If nothing is found, the string input is returned.
+     * </p>
+     *
+     * <pre>
+     * StringUtils.substringBefore(null, *)      = null
+     * StringUtils.substringBefore("", *)        = ""
+     * StringUtils.substringBefore("abc", 'a')   = ""
+     * StringUtils.substringBefore("abcba", 'b') = "a"
+     * StringUtils.substringBefore("abc", 'c')   = "ab"
+     * StringUtils.substringBefore("abc", 'd')   = "abc"
+     * </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 before the first occurrence of the separator, 
{@code null} if null String input
+     * @since 3.12.0
+     */
+    public static String substringBefore(final String str, final int 
separator) {
+        if (isEmpty(str)) {
+            return str;
+        }
+        final int pos = str.indexOf(separator);
+        if (pos == INDEX_NOT_FOUND) {
+            return str;
+        }
+        return str.substring(0, pos);
+    }
+
+    /**
      * <p>Gets the substring before the first occurrence of a separator.
      * The separator is not returned.</p>
      *
diff --git 
a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
index d876d7c..17c69fc 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
@@ -127,7 +127,24 @@ public class StringUtilsSubstringTest  {
         assertEquals(FOO, StringUtils.mid(FOOBAR, -1, 3));
     }
 
-    //-----------------------------------------------------------------------
+    @Test
+    public void testSubstringBefore_StringInt() {
+        assertEquals("foo", StringUtils.substringBefore("fooXXbarXXbaz", 'X'));
+
+        assertNull(StringUtils.substringBefore(null, 0));
+        assertNull(StringUtils.substringBefore(null, 'X'));
+        assertEquals("", StringUtils.substringBefore("", 0));
+        assertEquals("", StringUtils.substringBefore("", 'X'));
+
+        assertEquals("foo", StringUtils.substringBefore("foo", 0));
+        assertEquals("foo", StringUtils.substringBefore("foo", 'b'));
+        assertEquals("f", StringUtils.substringBefore("foot", 'o'));
+        assertEquals("", StringUtils.substringBefore("abc", 'a'));
+        assertEquals("a", StringUtils.substringBefore("abcba", 'b'));
+        assertEquals("ab", StringUtils.substringBefore("abc", 'c'));
+        assertEquals("abc", StringUtils.substringBefore("abc", 0));
+    }
+
     @Test
     public void testSubstringBefore_StringString() {
         assertEquals("foo", StringUtils.substringBefore("fooXXbarXXbaz", 
"XX"));
@@ -146,6 +163,7 @@ public class StringUtilsSubstringTest  {
         assertEquals("a", StringUtils.substringBefore("abcba", "b"));
         assertEquals("ab", StringUtils.substringBefore("abc", "c"));
         assertEquals("", StringUtils.substringBefore("abc", ""));
+        assertEquals("abc", StringUtils.substringBefore("abc", "X"));
     }
 
     @Test

Reply via email to