String length() Method:
The length of a string is total number of character that it holds. The Java String class has a method that returns the length of its instance.
Syntax:
Here is the syntax of the method:
public int length();
Note that there is no parameter and that the method is not static. This means that the method can only be invoked through a Java String instance.
The return value of the method is an int that represents the number of characters the String contains.
Example:
Here is an example that displays the length of the given string.
Test.java
public class Test
{
public static void main(String[] args)
{
String str =”testing123”;
System.out.println("The number of characters is: " + str.length());
}
}
OUTPUT of the program:
The number of characters is: 10
0 comments:
Post a Comment