Java String indexOf() Method

Java String类Java String Class


The indexOf() method has the following four forms:

  • public int indexOf(int ch):Returns the index of the first occurrence of the specified character in the string, or -1 if there is no such character in the string.

  • public int indexOf(int ch, int fromIndex):Returns the index of the first occurrence of the specified character in the string, starting the search from the fromIndex position, or -1 if there is no such character in the string.

  • int indexOf(String str):Returns the index of the first occurrence of the specified character in the string, or -1 if there is no such character in the string.

  • int indexOf(String str, int fromIndex):Returns the index of the first occurrence of the specified character in the string, starting the search from the fromIndex position, or -1 if there is no such character in the string.

Syntax

public int indexOf(int ch )

或

public int indexOf(int ch, int fromIndex)

或

int indexOf(String str)

或

int indexOf(String str, int fromIndex)

Parameters

  • ch-- A character, Unicode code.

  • fromIndex-- The index position from which to start the search. The first character is 0, the second is 1, and so on.

  • str-- The substring to search for.

Return Value

Find the position where a string, or a character's Unicode code, appears in the string:

Example 1

public class Main { public static void main(String args[]) { String string = "aaa456ac"; //Find the index of the specified character in the string. If present, return the index in the string; if not present, return -1. System.out.println(string.indexOf("b")); //indexOf(String str); return result: -1, "b" does not exist //Start searching from the fourth character position onward, including the current position. System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); return result: 6 //(Difference from before: the parameter above is of type String, the parameter below is of type int) Reference data: a-97, b-98, c-99 //Search from the beginning to see if the specified character exists. System.out.println(string.indexOf(99));//indexOf(int ch); return result: 7 System.out.println(string.indexOf('c'));//indexOf(int ch); return result: 7 //Search for ch from fromIndex. This is a character-type variable, not a string. The number corresponding to character 'a' is 97. System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); return result: 6 System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); return result: 6 } }

The output result is:

-1
6
7
7
6
6

The index of the first occurrence of the specified substring in the string, starting from the specified index.

Example 2

public class Test { public static void main(String args[]) { String Str = new String("Example Tutorial: www.example.com"); String SubStr1 = new String("example"); String SubStr2 = new String("com"); System.out.print("Find the position of the first occurrence of character o:" ); System.out.println(Str.indexOf( 'o' )); System.out.print("Find the position of the first occurrence of character o starting from the 14th position:" ); System.out.println(Str.indexOf( 'o', 14 )); System.out.print("The position of the first occurrence of substring SubStr1:" ); System.out.println( Str.indexOf( SubStr1 )); System.out.print("Search for the position of the first occurrence of substring SubStr1 starting from the fifteenth position:" ); System.out.println( Str.indexOf( SubStr1, 15 )); System.out.print("The position of the first occurrence of substring SubStr2:" ); System.out.println(Str.indexOf( SubStr2 )); } }

The result of executing the above program is:

查找字符 o 第一次出现的位置 :12
从第14个位置查找字符 o 第一次出现的位置 :17
子字符串 SubStr1 第一次出现的位置:9
从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :-1
子字符串 SubStr2 第一次出现的位置 :16

Java String类Java String Class

Other Extensions