Java startsWith() Method
The startsWith() method is used to test whether the string starts with the specified prefix.
Syntax
public boolean startsWith(String prefix, int toffset)
或
public boolean startsWith(String prefix)
Parameters
-
prefix -- The prefix.
-
toffset -- The position in the string at which to begin searching.
Return Value
Returns true if the string starts with the specified prefix; otherwise returns false.
Example
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
{
String
Str
=
new
String
(
"
www.runoob.com
"
)
;
System
.
out
.
(
"
Return value :
"
)
;
System
.
out
.
println
(
Str
.
startsWith
(
"
www
"
)
)
;
System
.
out
.
(
"
Return value :
"
)
;
System
.
out
.
println
(
Str
.
startsWith
(
"
runoob
"
)
)
;
System
.
out
.
(
"
Return value :
"
)
;
System
.
out
.
println
(
Str
.
startsWith
(
"
runoob
"
,
4
)
)
;
}
}
The execution result of the above program is:
返回值 :true
返回值 :false
返回值 :true
Java String class