Skip to content

Commit

Permalink
Add StringUtils.removeStart(String, char).
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Oct 8, 2021
1 parent b153aca commit e3e29d5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6158,6 +6158,36 @@ public static String removePattern(final String source, final String regex) {
return RegExUtils.removePattern(source, regex);
}

/**
* 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>
Expand Down
18 changes: 17 additions & 1 deletion src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,23 @@ public void testRemovePattern_StringString() {
}

@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, ""));
Expand Down

0 comments on commit e3e29d5

Please sign in to comment.