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

Java Examples – Finding the Last Occurrence of a String

Java Example – Find the last occurrence of a substring

Java Examples Java Examples

In the following example we use the string function strOrig.lastIndexOf(Stringname) to find the position where the substring Stringname occurs in strOrig:

The example code is as follows:

SearchlastString.java file

public

class

SearchlastString

{

public

static

void

main

(

String

[

]

args

)

{

String

strOrig

=

"

Hello world ,Hello Runoob

"

;

int

lastIndex

=

strOrig

.

lastIndexOf

(

"

Runoob

"

)

;

if

(

lastIndex

== -

1

)

{

System

.

out

.

println

(

"

The string Runoob was not found

"

)

;

}

else

{

System

.

out

.

println

(

"

The last occurrence position of the Runoob string:

"

lastIndex

)

;

}

}

}

The output of the above code example is:



Runoob 字符串最后出现的位置: 19

Java Examples Java Examples