Skip to content

StringUtils.substringBefore

Signature: StringUtils.substringBefore(inputString, separator)

Returns: String result - the substring before the first occurrence of the separator, or null if inputString is null

Gets the substring before the first occurrence of a separator. The separator is not returned. A null inputString will return null. An empty ("") inputString will return the empty string. A null separator will return the inputString. If nothing is found, the inputString is returned.

Parameters

Type Name Description Default
String inputString the String to get a substring from, may be null
String separator the String to search for, may be null

Example

// Typical usage: returns text before the FIRST occurrence of the separator (separator not included)
StringUtils.substringBefore("lion, and tiger, and bears, oh my!", "tiger");   // lion, and 

// If the separator is not found, returns the original inputString
StringUtils.substringBefore("lion, and tiger, and bears, oh my!", "not found"); // lion, and tiger, and bears, oh my!

// If inputString is empty, returns empty string
StringUtils.substringBefore("", "tiger");                                     // ""

// If inputString is null, returns null
StringUtils.substringBefore(null, "tiger");                                   // null

// If separator is null, returns the original inputString
StringUtils.substringBefore("lion, and tiger, and bears, oh my!", null);     // lion, and tiger, and bears, oh my!