Skip to content

StringUtils.substringsBetween

Signature: StringUtils.substringsBetween(inputString, open, close)

Returns: String[] result - the substrings, or null if no match

Searches the inputString for substrings delimited by a start and end tag, returning all matching substrings in an array. A null inputString will return null. A null open or close or an empty open or close will return null (no match).

Parameters

Type Name Description Default
String inputString the String containing the substring, may be null
String open the String before the substring, may be null
String close the String after the substring, may be null

Example

// Typical usage: returns ALL substrings found between open and close
StringUtils.substringsBetween(
    "lion, and tiger, and bears, oh my!",
    "lion",
    "bear"
); // [", and tiger, and "]

// If open or close is not found, returns null
StringUtils.substringsBetween(
    "lion, and tiger, and bears, oh my!",
    "not found",
    "zz"
); // null

// Multiple matches are returned in order
StringUtils.substringsBetween(
    "one and two and three and four and five",
    "and",
    "and"
 ); // [" two ", " four "]

// If inputString is null, returns null
StringUtils.substringsBetween(null, "[", "]"); // null

// If open or close is empty or null, returns null
StringUtils.substringsBetween("abc", "", "]");   // null
StringUtils.substringsBetween("abc", "[", null); // null