Here you can find the source of substringBetween(String str, String open, String close)
Parameter | Description |
---|---|
str | the String containing the substring, may be null |
open | the String before the substring, may be null |
close | the String after the substring, may be null |
null
if no match
public static String substringBetween(String str, String open, String close)
//package com.java2s; /*/* w w w .j a v a 2s. com*/ * (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 { /** * Gets the String that is nested in between two Strings. * Only the first match is returned. * * A <code>null</code> input String returns <code>null</code>. * A <code>null</code> open/close returns <code>null</code> (no match). * An empty ("") open and close returns an empty string. * * <pre> * StringUtils.substringBetween("w[ie]be", "[", "]") = "ie" * StringUtils.substringBetween(null, *, *) = null * StringUtils.substringBetween(*, null, *) = null * StringUtils.substringBetween(*, *, null) = null * StringUtils.substringBetween("", "", "") = "" * StringUtils.substringBetween("", "", "]") = null * StringUtils.substringBetween("", "[", "]") = null * StringUtils.substringBetween("wiebe", "", "") = "" * StringUtils.substringBetween("wiebe", "w", "e") = "ieb" * StringUtils.substringBetween("wiebewiebe", "w", "w") = "ieb" * </pre> * * @param str the String containing the substring, may be null * @param open the String before the substring, may be null * @param close the String after the substring, may be null * @return the substring, <code>null</code> if no match * @since 2.0 */ public static String substringBetween(String str, String open, String close) { if (str == null || open == null || close == null) { return null; } int start = str.indexOf(open); if (start != -1) { int end = str.indexOf(close, start + open.length()); if (end != -1) { return str.substring(start + open.length(), end); } } return null; } }