The indexof methods in java are: 1. [indexOf(String str)] returns the specified character str in the string; 2. [indexOf(String str, int index)] returns the specified character starting from the index position. str is the first index in the string.
The operating environment of this tutorial: windows7 system, java10 version, DELL G3 computer. This method is suitable for all brands of computers.
The indexof methods in java are:
1, indexOf(String str):
Returns the specified character str in the string (method caller ), or -1 if there is no such character in this string.
2, indexOf(String str, int index):
Returns the starting index of the first occurrence of the specified character str in the string starting from the index position. If this string If there is no such character, -1 is returned.
Example:
String str = "01234567890123456789"; System.out.println(str.indexOf("123")); System.out.println(str.indexOf("123",5));
The results of the above program are 1 and 11.
The indexOf() method in the first output function has only one parameter, so it returns the index of the first occurrence of 123 in str, which is 1; the indexOf() method in the first output function has Two parameters, the first parameter is the string 123 to be searched, and the second parameter is to start searching from the 5th position, so only the second 123 in str can be found, and the starting index of its occurrence is returned. , is 11.
Related free learning recommendations: java basic tutorial
The above is the detailed content of What are the indexof methods in java?. For more information, please follow other related articles on the PHP Chinese website!