StringUtils.indexOf¶
Signature: StringUtils.indexOf(inputString, searchString, startPosition*)
Returns: Integer index - the first index of the searchString or searchChar, -1 if no match or null inputString
Finds the first index within the inputString, handling null. A null inputString will return -1. A negative startPosition is treated as zero. An empty ("") searchString always matches. A startPosition greater than the inputString length only matches an empty searchString.
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 startPosition, negative treated as zero |
0 |
Example¶
// Examples of how to use the StringUtils.indexOf (0 based)
var input = "I am trying to find the number 17 starting spot1";
// Example: searchString as String
var firstIndexOfOne = StringUtils.indexOf(input, "1"); // 31
var firstIndexOfOneAfter40 = StringUtils.indexOf(input, "1", 40); // 47
var notFound = StringUtils.indexOf(input, "9"); // -1
// Example: searchString as char
var firstChar = StringUtils.indexOf(input, '1'); // 31
var firstCharAfter40 = StringUtils.indexOf(input, '1', 40); // 47
// Example: empty searchString
var emptySearch = StringUtils.indexOf(input, ""); // 0
// Example: null inputString
var nullResult = StringUtils.indexOf(null, "1"); // -1