Skip to content

Commit 731f99b

Browse files
authored
add StringUtils.ubstringBeforeLast(String, int)
Hello, This method with int param is missing :)
1 parent ff733d9 commit 731f99b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/main/java/org/apache/commons/lang3/StringUtils.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8690,6 +8690,41 @@ public static String substringBefore(final String str, final String separator) {
86908690
}
86918691
return str.substring(0, pos);
86928692
}
8693+
8694+
/**
8695+
* Gets the substring before the last occurrence of a separator.
8696+
* The separator is not returned.
8697+
*
8698+
* <p>A {@code null} string input will return {@code null}.
8699+
* An empty ("") string input will return the empty string.</p>
8700+
*
8701+
* <p>If nothing is found, the string input is returned.</p>
8702+
*
8703+
* <pre>
8704+
* StringUtils.substringBeforeLast(null, *) = null
8705+
* StringUtils.substringBeforeLast("", *) = ""
8706+
* StringUtils.substringBeforeLast("abcba", 'b') = "abc"
8707+
* StringUtils.substringBeforeLast("abc", 'c') = "ab"
8708+
* StringUtils.substringBeforeLast("a", 'a') = ""
8709+
* StringUtils.substringBeforeLast("a", 'z') = "a"
8710+
* </pre>
8711+
*
8712+
* @param str the String to get a substring from, may be null
8713+
* @param separator the character (Unicode code point) to search.
8714+
* @return the substring before the last occurrence of the separator,
8715+
* {@code null} if null String input
8716+
* @since 3.12.1
8717+
*/
8718+
public static String substringBeforeLast(final String str, final int separator) {
8719+
if (isEmpty(str)) {
8720+
return str;
8721+
}
8722+
final int pos = str.lastIndexOf(separator);
8723+
if (pos == INDEX_NOT_FOUND) {
8724+
return str;
8725+
}
8726+
return str.substring(0, pos);
8727+
}
86938728

86948729
/**
86958730
* Gets the substring before the last occurrence of a separator.

0 commit comments

Comments
 (0)