Here you can find the source of substringBetween(String str, String open, String close)
Gets the String that is nested in between two Strings.
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; //License from project: Apache License public class Main { private static final int INDEX_NOT_FOUND = -1; /**//from ww w .j a va2 s . co m * <p> * Gets the String that is nested in between two Strings. Only the first * match is returned. * </p> * <p> * <p> * 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. * </p> * <p> * <pre> * StringUtils.substringBetween("wx[b]yz", "[", "]") = "b" * StringUtils.substringBetween(null, *, *) = null * StringUtils.substringBetween(*, null, *) = null * StringUtils.substringBetween(*, *, null) = null * StringUtils.substringBetween("", "", "") = "" * StringUtils.substringBetween("", "", "]") = null * StringUtils.substringBetween("", "[", "]") = null * StringUtils.substringBetween("yabcz", "", "") = "" * StringUtils.substringBetween("yabcz", "y", "z") = "abc" * StringUtils.substringBetween("yabczyabcz", "y", "z") = "abc" * </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 != INDEX_NOT_FOUND) { int end = str.indexOf(close, start + open.length()); if (end != INDEX_NOT_FOUND) { return str.substring(start + open.length(), end); } } return null; } }