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 e3e29d5 Add StringUtils.removeStart(String, char). e3e29d5 is described below commit e3e29d585a455693829deb9231440aeda38eacd4 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Oct 8 14:28:50 2021 -0400 Add StringUtils.removeStart(String, char). --- src/changes/changes.xml | 1 + .../java/org/apache/commons/lang3/StringUtils.java | 30 ++++++++++++++++++++++ .../org/apache/commons/lang3/StringUtilsTest.java | 18 ++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 8911adc..633b43d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -84,6 +84,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_16.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add ObjectUtils.identityHashCodeHex(Object).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add ObjectUtils.hashCodeHex(Object).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add StringUtils.removeStart(String, char).</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.2.3 #735.</action> <action type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, #764.</action> diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index b716711..e7d5ca3 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -6159,6 +6159,36 @@ public class StringUtils { } /** + * Removes a char only if it is at the beginning of a source string, + * otherwise returns the source string. + * + * <p>A {@code null} source string will return {@code null}. + * An empty ("") source string will return the empty string. + * A {@code null} search char will return the source string.</p> + * + * <pre> + * StringUtils.removeStart(null, *) = null + * StringUtils.removeStart("", *) = "" + * StringUtils.removeStart(*, null) = * + * StringUtils.removeStart("/path", '/') = "path" + * StringUtils.removeStart("path", '/') = "path" + * StringUtils.removeStart("path", 0) = "path" + * </pre> + * + * @param str the source String to search, may be null. + * @param remove the char to search for and remove. + * @return the substring with the char removed if found, + * {@code null} if null String input. + * @since 3.13.0 + */ + public static String removeStart(final String str, final char remove) { + if (isEmpty(str)) { + return str; + } + return str.charAt(0) == remove ? str.substring(1) : str; + } + + /** * <p>Removes a substring only if it is at the beginning of a source string, * otherwise returns the source string.</p> * diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 5750f8b..e501982 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -1794,7 +1794,23 @@ public class StringUtilsTest { } @Test - public void testRemoveStart() { + public void testRemoveStartChar() { + // StringUtils.removeStart("", *) = "" + assertNull(StringUtils.removeStart(null, '\0')); + assertNull(StringUtils.removeStart(null, 'a')); + + // StringUtils.removeStart(*, null) = * + assertEquals(StringUtils.removeStart("", '\0'), ""); + assertEquals(StringUtils.removeStart("", 'a'), ""); + + // All others: + assertEquals(StringUtils.removeStart("/path", '/'), "path"); + assertEquals(StringUtils.removeStart("path", '/'), "path"); + assertEquals(StringUtils.removeStart("path", '\0'), "path"); + } + + @Test + public void testRemoveStartString() { // StringUtils.removeStart("", *) = "" assertNull(StringUtils.removeStart(null, null)); assertNull(StringUtils.removeStart(null, ""));