Skip to content

StringUtils.substringBetween

Signature: StringUtils.substringBetween(inputString, open, close*)

Returns: String result - the substring, or null if no match

Gets the String that is nested in between two Strings. Only the first match is returned. A null inputString will return null. A null open or close will return null (no match). An empty open and close will return an empty string.

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* (optional) the String after the substring, may be null null

Example

// Typical usage: returns text between the first matching open and close strings
StringUtils.substringBetween(
    "lion, and tiger, and bears, oh my!",
    "lion",
    "bear"
); // , and tiger, and 

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

// Only the FIRST match is returned
StringUtils.substringBetween(
    "one and two and three and four and five",
    "and",
    "and"
); //  two