Here you can find the source of substringAfter(String template, String toFind, String defaultTo)
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. |
public static String substringAfter(String template, String toFind, String defaultTo)
//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; } }