Java Examples - String Search
The following example uses the indexOf() method of the String class to find the position where a substring occurs in a string; if it exists, it returns the position (starting from 0); if not, it returns -1:
SearchStringEmp.java file
public
class
SearchStringEmp
{
public
static
void
main
(
String
[
]
args
)
{
String
strOrig
=
"
Google Runoob Taobao
"
;
int
intIndex
=
strOrig
.
indexOf
(
"
Runoob
"
)
;
if
(
intIndex
== -
1
)
{
System
.
out
.
println
(
"
The string Runoob was not found
"
)
;
}
else
{
System
.
out
.
println
(
"
Position of the Runoob string
"
intIndex
)
;
}
}
}
The output of the above code example is:
Runoob 字符串位置 7
Java Examples