add StringUtils.ubstringBeforeLast(String, int)

Hello,
This method with int param is missing :)
This commit is contained in:
Michele Mariotti 2022-10-09 11:44:57 +02:00 committed by GitHub
parent ff733d9d9b
commit 731f99b82b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 0 deletions

View File

@ -8690,6 +8690,41 @@ public class StringUtils {
}
return str.substring(0, pos);
}
/**
* Gets the substring before the last occurrence of a separator.
* The separator is not returned.
*
* <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.substringBeforeLast(null, *) = null
* StringUtils.substringBeforeLast("", *) = ""
* StringUtils.substringBeforeLast("abcba", 'b') = "abc"
* StringUtils.substringBeforeLast("abc", 'c') = "ab"
* StringUtils.substringBeforeLast("a", 'a') = ""
* StringUtils.substringBeforeLast("a", 'z') = "a"
* </pre>
*
* @param str the String to get a substring from, may be null
* @param separator the character (Unicode code point) to search.
* @return the substring before the last occurrence of the separator,
* {@code null} if null String input
* @since 3.12.1
*/
public static String substringBeforeLast(final String str, final int separator) {
if (isEmpty(str)) {
return str;
}
final int pos = str.lastIndexOf(separator);
if (pos == INDEX_NOT_FOUND) {
return str;
}
return str.substring(0, pos);
}
/**
* Gets the substring before the last occurrence of a separator.