Home  >  Article  >  Java  >  What are the indexof methods in java?

What are the indexof methods in java?

coldplay.xixi
coldplay.xixiOriginal
2021-02-22 15:24:5021518browse

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.

What are the indexof methods in java?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn