Java String Sub String substringAfterLast(final String str, final String separator)

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

Description

Gets the substring after the last occurrence of a separator.

License

Apache 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 after the last occurrence of the separator, null if null String input

Declaration

public static String substringAfterLast(final String str, final String separator) 

Method Source Code

//package com.java2s;
/*//from  w  w w .  j  ava 2 s . c  o  m
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
 * applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */

public class Main {
    /**
     * <p>
     * Gets the substring after the last 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. An empty or {@code null} separator will
     * return the empty string if the input string is not {@code null}.
     * </p>
     *
     * <p>
     * If nothing is found, the empty string is returned.
     * </p>
     *
     * <pre>
     * StringUtils.substringAfterLast(null, *)      = null
     * StringUtils.substringAfterLast("", *)        = ""
     * StringUtils.substringAfterLast(*, "")        = ""
     * StringUtils.substringAfterLast(*, null)      = ""
     * StringUtils.substringAfterLast("abc", "a")   = "bc"
     * StringUtils.substringAfterLast("abcba", "b") = "a"
     * StringUtils.substringAfterLast("abc", "c")   = ""
     * StringUtils.substringAfterLast("a", "a")     = ""
     * StringUtils.substringAfterLast("a", "z")     = ""
     * </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 after the last occurrence of the separator,
     *         {@code null} if null String input
     * @since 2.0
     */
    public static String substringAfterLast(final String str, final String separator) {
        if (isEmpty(str)) {
            return str;
        }
        if (isEmpty(separator)) {
            return "";
        }
        final int pos = str.lastIndexOf(separator);
        if (pos == -1 || pos == str.length() - separator.length()) {
            return "";
        }
        return str.substring(pos + separator.length());
    }

    /**
     * Check whether the given String is empty.
     */
    public static boolean isEmpty(Object str) {
        return str == null || "".equals(str);
    }
}

Related

  1. substringAfter(String value, char delim)
  2. substringAfterFirstIndex(String value)
  3. substringAfterIfExist(String source, String rev)
  4. subStringAfterIgnoreCase(String str, String separator, Integer num)
  5. substringAfterLast(final String str, final String separator)
  6. substringAfterLast(final String value, final String searchValue)
  7. substringAfterLast(String input, String delimiter)
  8. substringAfterLast(String str, String pattern)
  9. substringAfterLast(String str, String separator)