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

Java String length() Method

Java String length() Method

Java String ClassJava String class


The length() method is used to return the length of the string.

The length of an empty string is 0.

Syntax


public int length()

Parameters

Return Value

Returns the string length.

Example

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

Example

public

class

Test

{

       

public

static

void

main

(

String

args

[

]

)

{

               

String

Str1

=

new

String

(

"www.runoob.com"

)

;

               

String

Str2

=

new

String

(

"runoob"

)

;

               

System

.

out

.

print

(

"String Str1 length :"

)

;

               

System

.

out

.

println

(

Str1.

length

(

)

)

;

               

System

.

out

.

print

(

"String Str2 length :"

)

;

               

System

.

out

.

println

(

Str2.

length

(

)

)

;

       

}

}

The execution result of the above program is:


字符串 Str1 长度 :14
字符串 Str2 长度 :6

Java String ClassJava String class