Java String length() Method
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
- None
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
.
(
"String Str1 length :"
)
;
System
.
out
.
println
(
Str1.
length
(
)
)
;
System
.
out
.
(
"String Str2 length :"
)
;
System
.
out
.
println
(
Str2.
length
(
)
)
;
}
}
The execution result of the above program is:
字符串 Str1 长度 :14
字符串 Str2 长度 :6
Java String class