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. 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

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🙂.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.