Java String Sub String substringBetween(String str, String open, String close)

Here you can find the source of substringBetween(String str, String open, String close)

Description

Gets the String that is nested in between two Strings.

License

Apache License

Parameter

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

Return

the substring, null if no match

Declaration

public static String substringBetween(String str, String open, String close) 

Method Source Code

//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;
    }
}

Related

  1. substringBetween(String line, final String delimiterBefore, final String delimiterAfter)
  2. substringBetween(String s, String part1, String part2)
  3. substringBetween(String s, String part1, String part2)
  4. substringBetween(String source, String strBegin, String strEnd)
  5. substringBetween(String str, String before, String after)
  6. substringBetween(String str, String open, String close)
  7. substringBetween(String str, String open, String close)
  8. substringBetween(String str, String open, String close, int fromIndex)
  9. substringBetween(String str, String pos1, String pos2)