Java Reference ยท java.lang
Java String.indexOf() method
Reference for Java String.indexOf() and lastIndexOf(): find a character or substring, the -1 result, start offsets and finding every occurrence.
String.indexOf() searches a string for a character or a substring and returns the zero-based index of the first match, or -1 if there is none. lastIndexOf() does the same from the end. Both are case-sensitive and match literal text, not regular expressions.
Each method has versions that take a char (given as an int code point) or a String, and an optional starting position, so you can step through all matches in a loop.
"banana".indexOf("an"); // 1 "banana".lastIndexOf("an"); // 3 "banana".indexOf('z'); // -1
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public int indexOf(int ch) public int indexOf(int ch, int fromIndex) public int indexOf(String str) public int indexOf(String str, int fromIndex) public int lastIndexOf(int ch) public int lastIndexOf(int ch, int fromIndex) public int lastIndexOf(String str) public int lastIndexOf(String str, int fromIndex)
| Name | Description |
|---|---|
ch |
A character, passed as an int code point (a char widens automatically). |
str |
The substring to look for. |
fromIndex |
Where to start. For indexOf, a negative value acts as 0 and a value past the end finds nothing. For lastIndexOf, the search runs backward from this index. |
Returns: the index of the first (or last) match, or -1.
Throws: NullPointerException if str or the receiver is null. No exception for out-of-range fromIndex.
Java 21 added indexOf(int ch, int beginIndex, int endIndex) and indexOf(String str, int beginIndex, int endIndex) to search a bounded range.
Usage notes
- Always test for
-1before using the result as an index; passing-1tosubstring()throws. indexOf("")returns0(orfromIndexclamped to the length).- Case-sensitive. For case-insensitive search, lower-case both sides, or use
regionMatches(true, ...)or a regex withPattern.CASE_INSENSITIVE. - To only ask yes or no, contains() reads better than
indexOf(x) >= 0. - Stepping through matches: advance
fromIndexby at least 1 (use the match length for non-overlapping, 1 for overlapping matches). - Performance. A straightforward scan, O(n * m) worst case, usually near O(n) and heavily optimized by the JIT for single characters.
- Not a regex. Characters like
.and*have no special meaning here; use split() orPatternfor patterns. - Code points. Passing an
intabove0xFFFFsearches for the supplementary character as a surrogate pair.