StringUtils.lastIndexOf¶
Signature: StringUtils.lastIndexOf(inputString, searchString, startPosition*)
Returns: Integer index - the last index of the searchString or searchChar, -1 if no match or null inputString
Finds the last index within the inputString, handling null. A null inputString will return -1. A negative startPosition returns -1. An empty ("") searchString always matches unless the startPosition is negative. A startPosition greater than the inputString length searches the whole string.
Parameters¶
| Type | Name | Description | Default |
|---|---|---|---|
| String | inputString | the String to check, may be null | |
| String or char | searchString | the String or character to find, may be null | |
| Integer | startPosition* | (optional) the start position, negative treated as zero | 0 |
Example¶
// Examples of how to use the StringUtils.lastIndexOf (0 based)
var input = "one two three two one";
// Example: searchString as String
var lastTwo = StringUtils.lastIndexOf(input, "two"); // 14
var lastTwoBefore10 = StringUtils.lastIndexOf(input, "two", 10); // 4
var notFound = StringUtils.lastIndexOf(input, "xyz"); // -1
// Example: searchString as char
var lastChar = StringUtils.lastIndexOf(input, 'o'); // 18
var lastCharBefore10 = StringUtils.lastIndexOf(input, 'o', 10); // 6
// Example: empty searchString
var emptySearch = StringUtils.lastIndexOf(input, ""); // 21 (length of string)
// Example: null inputString
var nullResult = StringUtils.lastIndexOf(null, "two"); // -1
// Example: negative startPosition
var negativeStart = StringUtils.lastIndexOf(input, "two", -1); // -1