To get the length of a text specified in a String
object we can use the String.length()
method. This method returns the string length as an int
value. The length of the string is equals to the length of the sequence of characters represented by this string object.
package org.kodejava.example.lang;
public class StringLengthExample {
public static void main(String[] args) {
String name = "Kodejava";
// Get the length of the string specified by the name
// variable.
int length = name.length();
System.out.println("Length = " + length);
}
}
If you run this code you will get the following output:
Length = 8
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020