How do I know the length of a string?

To get the length of a text specified in a String object we can use the String.length() method call. 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

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply