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

Java String isEmpty() Method

Java String isEmpty() Method

Java String ClassJava String class


The isEmpty() method is used to determine whether the string is empty.

Syntax


public boolean isEmpty()

Parameters

Return Value

Returns true if the string is empty, otherwise returns false.

The string uses the length() method to compute its length; if it returns 0, the string is empty.

Example

The following example demonstrates the use of the isEmpty() method:

Example

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

String

myStr1

=

"

Runoob

"

;

String

myStr2

=

"

"

;

//

Empty string

String

myStr3

=

"

"

;

//

Multiple spaces, length() is not 0

System

.

out

.

println

(

"

Whether myStr1 is empty:

"

myStr1

.

isEmpty

(

)

)

;

System

.

out

.

println

(

"

Whether myStr2 is empty:

"

myStr2

.

isEmpty

(

)

)

;

System

.

out

.

println

(

"

myStr3 length:

"

myStr3

.

length

(

)

)

;

System

.

out

.

println

(

"

Whether myStr3 is empty:

"

myStr3

.

isEmpty

(

)

)

;

}

}

The execution result of the above program is:


myStr1 是否为空:false
myStr2 是否为空:true
myStr3 长度:4
myStr3 是否为空:false

Java String ClassJava String class