Java String indexOf() Method
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 the character does not occur in this 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 at fromIndex, or -1 if the character does not occur in this string.
-
int indexOf(String str): Returns the index of the first occurrence of the specified character in the string, or -1 if the character does not occur in this string.
-
int indexOf(String str, int fromIndex): Returns the index of the first occurrence of the specified character in the string, starting the search at fromIndex, or -1 if the character does not occur in this 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 -- The character, as a Unicode code point.
-
fromIndex -- The index position at which to start searching; 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 the Unicode code of a character, occurs in the string:
Example 1
public
class
Main
{
public
static
void
main
(
String
args
[
]
)
{
String
string
=
"
aaa456ac
"
;
//
Finds the index of the specified character in the string. If present, returns its index in the string; if not, returns -1.
System
.
out
.
println
(
string
.
indexOf
(
"
b
"
)
)
;
//
indexOf(String str); returned result: -1, "b" does not exist
//
Continue searching from the fourth character position onward, including the current position
System
.
out
.
println
(
string
.
indexOf
(
"
a
"
,
3
)
)
;
//
indexOf(String str, int fromIndex); returned result: 6
//
(Difference from the previous one: the parameter above is of type String, while the parameter below is of type int) Reference data: a-97, b-98, c-99
//
Search from the beginning for the existence of the specified character
System
.
out
.
println
(
string
.
indexOf
(
99
)
)
;
//
indexOf(int ch); returned result: 7
System
.
out
.
println
(
string
.
indexOf
(
'
c
'
)
)
;
//
indexOf(int ch); returned result: 7
//
Search for ch starting from fromIndex; this is a character variable, not a string. The number corresponding to the character a is 97.
System
.
out
.
println
(
string
.
indexOf
(
97
,
3
)
)
;
//
indexOf(int ch, int fromIndex); returned result: 6
System
.
out
.
println
(
string
.
indexOf
(
'
a
'
,
3
)
)
;
//
indexOf(int ch, int fromIndex); returned result: 6
}
}
The output is:
-1
6
7
7
6
6
Returns 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
(
"
Runoob Tutorial:www.runoob.com
"
)
;
String
SubStr1
=
new
String
(
"
runoob
"
)
;
String
SubStr2
=
new
String
(
"
com
"
)
;
System
.
out
.
(
"
Find the position of the first occurrence of the character o :
"
)
;
System
.
out
.
println
(
Str
.
indexOf
(
'
o
'
)
)
;
System
.
out
.
(
"
Find the position of the first occurrence of the character o starting from position 14 :
"
)
;
System
.
out
.
println
(
Str
.
indexOf
(
'
o
'
,
14
)
)
;
System
.
out
.
(
"
The position of the first occurrence of the substring SubStr1:
"
)
;
System
.
out
.
println
(
Str
.
indexOf
(
SubStr1
)
)
;
System
.
out
.
(
"
Search for the position of the first occurrence of the substring SubStr1 starting from position 15 :
"
)
;
System
.
out
.
println
(
Str
.
indexOf
(
SubStr1
,
15
)
)
;
System
.
out
.
(
"
The position of the first occurrence of the substring SubStr2 :
"
)
;
System
.
out
.
println
(
Str
.
indexOf
(
SubStr2
)
)
;
}
}
The execution result of the above program is:
查找字符 o 第一次出现的位置 :12
从第14个位置查找字符 o 第一次出现的位置 :17
子字符串 SubStr1 第一次出现的位置:9
从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :-1
子字符串 SubStr2 第一次出现的位置 :16
Java String class