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

public class StringLengthExample {
    public static void main(String[] args) {
        String name = "Kode Java - Learn Java by Examples";

        // 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 = 34

Leave a Reply

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