Skip to content

StringUtils.substringAfterLast

Signature: StringUtils.substringAfterLast(inputString, separator)

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

Gets the substring after the last occurrence of a separator. The separator is not returned. A null inputString will return null. An empty ("") inputString will return the empty string. An empty or null separator will return the empty string if the inputString is not null. If nothing is found, the empty string 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 after the LAST occurrence of the separator (separator not included)
StringUtils.substringAfterLast("lion, and tiger, and bears, oh my!", ", ");    // oh my!

// If the separator is not found, returns the empty string
StringUtils.substringAfterLast("lion, and tiger, and bears, oh my!", "not found"); // ""

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

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

// If separator is empty or null (and inputString is not), returns empty string
StringUtils.substringAfterLast("lion, and tiger, and bears, oh my!", "");     // ""
StringUtils.substringAfterLast("lion, and tiger, and bears, oh my!", null);   // ""