Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java String indexOf() Method

Java String indexOf() Method

Java String ClassJava String class


The indexOf() method has the following four forms:

Syntax


public int indexOf(int ch )

或

public int indexOf(int ch, int fromIndex)

或

int indexOf(String str)

或

int indexOf(String str, int fromIndex)

Parameters

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

.

print

(

"

Find the position of the first occurrence of the character o :

"

)

;

System

.

out

.

println

(

Str

.

indexOf

(

'

o

'

)

)

;

System

.

out

.

print

(

"

Find the position of the first occurrence of the character o starting from position 14 :

"

)

;

System

.

out

.

println

(

Str

.

indexOf

(

'

o

'

,

14

)

)

;

System

.

out

.

print

(

"

The position of the first occurrence of the substring SubStr1:

"

)

;

System

.

out

.

println

(

Str

.

indexOf

(

SubStr1

)

)

;

System

.

out

.

print

(

"

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

.

print

(

"

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 ClassJava String class