Here you can find the source of substringAfterLast(final String str, final String separator)
Gets the substring after 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 |
public static String substringAfterLast(final String str, final String separator)
//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); } }