Java String Sub String substringAfter(String template, String toFind, String defaultTo)

Here you can find the source of substringAfter(String template, String toFind, String defaultTo)

Description

Searches a given string ( template ) for a substring ( toFind ) and returns the portion of the string that follows the substring.

License

Apache License

Parameter

Parameter Description
template The string to search in.
toFind The substring to search for.
defaultTo The alternative string to return if toFind is not found.

Return

The substring of template that follows toFind , or defaultTo if toFind is not found.

Declaration

public static String substringAfter(String template, String toFind, String defaultTo) 

Method Source Code

//package com.java2s;
/*// w w  w . ja  v a  2  s.  c  o m
 * Copyright 2012 Splunk, Inc.
 *
 * 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 {
    /**
     * Searches a given string ({@code template}) for a substring 
     * ({@code toFind}) and returns the portion of the string that follows the
     * substring. If the {@code toFind} string is not found in {@code template},
     * an alternative string ({@code defaultTo}) is returned. 
     * <br>
     * For example: 
     * <br>
     * {@code substringAfter("This is a test", "is a ", "abcd")} 
     * returns {@code "test"}. 
     * <br>
     * {@code substringAfter("This is a test", "boris",
     * "abcd")} returns {@code "abcd"}.
     *
     * @param template The string to search in.
     * @param toFind The substring to search for.
     * @param defaultTo The alternative string to return if {@code toFind} is 
     *        not found.
     * @return The substring of {@code template} that follows {@code toFind},
     *         or {@code defaultTo} if {@code toFind} is not found.
     */
    public static String substringAfter(String template, String toFind, String defaultTo) {
        int toFindLength = toFind.length();
        int toFindOffset = template.indexOf(toFind);
        int substringOffset = toFindOffset + toFindLength;
        String returnValue;
        if (toFindOffset == -1) { // toFind not found in template
            returnValue = defaultTo;
        } else {
            returnValue = template.substring(substringOffset);
        }
        return returnValue;
    }
}

Related

  1. substringAfter(String str, String separator)
  2. substringAfter(String str, String separator)
  3. substringAfter(String str, String separator)
  4. substringAfter(String string, String delim)
  5. substringAfter(String string, String delimiter)
  6. substringAfter(String text, int index)
  7. substringAfter(String text, String after)
  8. substringAfter(String value, char delim)
  9. substringAfterFirstIndex(String value)