Here you can find the source of substringBeforeLast(String str, String separator)
Gets the substring before the last occurrence of a separator.
Parameter | Description |
---|---|
str | the String to get a substring from, may be null |
separator | the String to search for, may be null |
null
if null String input
public static String substringBeforeLast(String str, String separator)
//package com.java2s; public class Main { /**/* ww w.ja v a 2 s.co m*/ * <p> * Gets the substring before the last occurrence of a separator. The * separator is not returned. * </p> * * <p> * A <code>null</code> string input will return <code>null</code>. An empty * ("") string input will return the empty string. An empty or * <code>null</code> separator will return the input string. * </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" * StringUtils.substringBeforeLast("a", null) = "a" * StringUtils.substringBeforeLast("a", "") = "a" * </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 last occurrence of the separator, * <code>null</code> if null String input * @since 2.0 */ public static String substringBeforeLast(String str, String separator) { if (isEmpty(str) || isEmpty(separator)) { return str; } int pos = str.lastIndexOf(separator); if (pos == -1) { return str; } return str.substring(0, pos); } /** * <p> * Checks if a String is empty ("") or null. * </p> * * <pre> * StringUtils.isEmpty(null) = true * StringUtils.isEmpty("") = true * StringUtils.isEmpty(" ") = false * StringUtils.isEmpty("bob") = false * StringUtils.isEmpty(" bob ") = false * </pre> * * <p> * NOTE: This method changed in Lang version 2.0. It no longer trims the * String. That functionality is available in isBlank(). * </p> * * @param str * the String to check, may be null * @return <code>true</code> if the String is empty or null */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } }