Android String Sub String Get 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   ww w  .ja v  a  2 s.  c o m*/
 * (C) Copyright Itude Mobile B.V., The Netherlands
 * 
 * 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>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>
     *
     * <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());
    }

    /**
     * <p>Checks if a String is empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("wiebe")     = false
     * StringUtils.isEmpty("  wiebe  ") = false
     * </pre>
     *
     * @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. substringBefore(String str, String separator)
  2. substringBeforeLast(String str, String separator)
  3. unicodePreservingSubstring(String paramString, int paramInt1, int paramInt2)
  4. unicodePreservingSubstring(String str, int begin, int end)
  5. unicodePreservingSubstring(String str, int begin)
  6. substringBetween(String str, String open, String close)
  7. subStringEndString(String sourceStr, String endString)
  8. hightLinghtString(String destStr, String subStr, int color)
  9. replace(String source, String subject, String object)