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

Java startsWith() Method

Java startsWith() Method

Java String ClassJava String class


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

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

.

print

(

"

Return value :

"

)

;

System

.

out

.

println

(

Str

.

startsWith

(

"

www

"

)

)

;

System

.

out

.

print

(

"

Return value :

"

)

;

System

.

out

.

println

(

Str

.

startsWith

(

"

runoob

"

)

)

;

System

.

out

.

print

(

"

Return value :

"

)

;

System

.

out

.

println

(

Str

.

startsWith

(

"

runoob

"

,

4

)

)

;

}

}

The execution result of the above program is:


返回值 :true
返回值 :false
返回值 :true

Java String ClassJava String class