Java Example – Find the last occurrence of a substring
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