Add StringUtils.substringBefore(String, int).
This commit is contained in:
parent
92529ed903
commit
4f85c164a1
|
@ -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 FailableShortSupplier, handy for JDBC APIs.</action>
|
||||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_17.</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 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 -->
|
<!-- UPDATES -->
|
||||||
<action type="update" dev="ggregory" due-to="Gary Gregory">Enable Dependabot #587.</action>
|
<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>
|
<action type="update" dev="chtompki">Bump junit-jupiter from 5.6.2 to 5.7.0.</action>
|
||||||
|
|
|
@ -8702,6 +8702,44 @@ public static String substringAfterLast(final String str, final String separator
|
||||||
return str.substring(pos + separator.length());
|
return str.substring(pos + separator.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <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.
|
* <p>Gets the substring before the first occurrence of a separator.
|
||||||
* The separator is not returned.</p>
|
* The separator is not returned.</p>
|
||||||
|
|
|
@ -127,7 +127,24 @@ public void testMid_String() {
|
||||||
assertEquals(FOO, StringUtils.mid(FOOBAR, -1, 3));
|
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
|
@Test
|
||||||
public void testSubstringBefore_StringString() {
|
public void testSubstringBefore_StringString() {
|
||||||
assertEquals("foo", StringUtils.substringBefore("fooXXbarXXbaz", "XX"));
|
assertEquals("foo", StringUtils.substringBefore("fooXXbarXXbaz", "XX"));
|
||||||
|
@ -146,6 +163,7 @@ public void testSubstringBefore_StringString() {
|
||||||
assertEquals("a", StringUtils.substringBefore("abcba", "b"));
|
assertEquals("a", StringUtils.substringBefore("abcba", "b"));
|
||||||
assertEquals("ab", StringUtils.substringBefore("abc", "c"));
|
assertEquals("ab", StringUtils.substringBefore("abc", "c"));
|
||||||
assertEquals("", StringUtils.substringBefore("abc", ""));
|
assertEquals("", StringUtils.substringBefore("abc", ""));
|
||||||
|
assertEquals("abc", StringUtils.substringBefore("abc", "X"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue