java indexof
时间: 2023-09-11 07:04:27 浏览: 100
The `indexOf()` method in Java is used to find the index of the first occurrence of a given character or substring in a given string.
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)
```
Parameters:
- `ch` - The character to search for
- `fromIndex` - The index from where to start the search
- `str` - The substring to search for
Return Value:
- The index of the first occurrence of the given character or substring in the string, or -1 if not found.
Example:
```
String str = "Hello World";
int index = str.indexOf('o');
System.out.println("Index of 'o': " + index); // Output: Index of 'o': 4
index = str.indexOf("World");
System.out.println("Index of 'World': " + index); // Output: Index of 'World': 6
```
阅读全文