Java String Sub String substringBeforeLast(String str, String separator)

Here you can find the source of substringBeforeLast(String str, String separator)

Description

Gets the substring before the last occurrence of a separator.

License

Open Source License

Parameter

Parameter Description
str the String to get a substring from, may be null
separator the String to search for, may be null

Return

the substring before the last occurrence of the separator, null if null String input

Declaration

public static String substringBeforeLast(String str, String separator) 

Method Source Code

//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;
    }
}

Related

  1. substringBeforeFirst(String string, String delimiter)
  2. subStringBeforeFirstTab(final String s)
  3. substringBeforeLast(final String str, final String separator)
  4. subStringBeforeLast(String str, String separator)
  5. substringBeforeLast(String str, String separator)
  6. substringBeforeLast(String text, String separator)
  7. substringBeforeLastChar(String str, String separator)
  8. substringBeforeLastIgnoreCase(final String target, final String separator)
  9. substringBetween(final String s, final String tag)