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

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

Description

Gets the substring after the first 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 first occurrence of the separator, null if null String input

Declaration

public static String substringAfter(String str, String separator) 

Method Source Code

//package com.java2s;
/**//from   www  . jav  a  2s .  c om
 *    Copyright 2013 MegaFon
 *
 *    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 {
    public static final String EMPTY = "";

    /**
     * <p>Gets the substring after the first occurrence of a separator.
     * The separator is not returned.</p>
     * <p/>
     * <p>A <code>null</code> string input will return <code>null</code>.
     * An empty ("") string input will return the empty string.
     * A <code>null</code> separator will return the empty string if the
     * input string is not <code>null</code>.</p>
     * <p/>
     * <pre>
     * StringUtils.substringAfter(null, *)      = null
     * StringUtils.substringAfter("", *)        = ""
     * StringUtils.substringAfter(*, null)      = ""
     * StringUtils.substringAfter("abc", "a")   = "bc"
     * StringUtils.substringAfter("abcba", "b") = "cba"
     * StringUtils.substringAfter("abc", "c")   = ""
     * StringUtils.substringAfter("abc", "d")   = ""
     * StringUtils.substringAfter("abc", "")    = "abc"
     * </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 first occurrence of the separator,
     *         <code>null</code> if null String input
     * @since 2.0
     */
    public static String substringAfter(String str, String separator) {
        if (isEmpty(str)) {
            return str;
        }
        if (separator == null) {
            return EMPTY;
        }
        int pos = str.indexOf(separator);
        if (pos == -1) {
            return EMPTY;
        }
        return str.substring(pos + separator.length());
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}

Related

  1. substringAfter(String s, String cs)
  2. substringAfter(String s, String substr, boolean fromend)
  3. substringAfter(String sourceStr, String expr)
  4. substringAfter(String str, int beginChar, boolean last)
  5. substringAfter(String str, String delimiter)
  6. substringAfter(String str, String separator)
  7. substringAfter(String str, String separator)
  8. substringAfter(String string, String delim)
  9. substringAfter(String string, String delimiter)