Java StringBuilder indexOf() Method
Example
Find the position of the first "o":
StringBuilder sb = new StringBuilder("Hello World");
System.out.println(sb.indexOf("o"));
Definition and Usage
The indexOf() method returns the position of the first occurrence of the specified text. It returns -1 if the text is not found.
Tip: Use lastIndexOf() to find the last occurrence.
Syntax
One of the following:
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
Parameter Values
| Parameter | Description |
|---|---|
| str | Required. The text to search for. |
| fromIndex | Optional. The index to start the search from. |
Technical Details
| Returns: | An int value, representing the position of the first occurrence, or -1 if it never occurs. |
|---|
Related Pages
❮ StringBuilder Methods