Using String Length Method in Java

To get the length of a string in Java, use the string.length() method. It returns the number of characters in the string as an int. Spaces and punctuation marks are counted as characters, and an empty string has a length of 0. Non-English letters, such as Cyrillic or Greek, count as one character, and most emoji count as two. In this Java String Length Example, we get the length of a string of 13 characters. Click Execute to run the Java String Length Example online and see the result.
Using String Length Method in Java Execute
public class StringLengthDemo {
    public static void main(String[] args) {
        String example = "Hello, World!";
        
        int stringLength = example.length();
        
        System.out.println("The length of the string '" + example + "' is: " + stringLength);
    }
}

Java String length() Syntax

length() is a method of the String class. It takes no arguments and returns an int.

Java String length() Syntax
public int length()

Java String Length Examples

The following are examples of finding the length of a string in Java. You can run any of these examples in the online Java code runner and paste your own text to count its characters.

Get the length of a string

Every character counts, including spaces. The string "Java is fun" has 9 letters and 2 spaces.

Java String length() Example
public class StringLengthExample {
    public static void main(String[] args) {
        String word = "Hello";
        String sentence = "Java is fun";
        System.out.println(word.length());
        System.out.println(sentence.length());
    }
}

// output:
// 5
// 11

Check if a string is empty

isEmpty() returns true when length() is 0. isBlank() also returns true for a string that holds only spaces.

Java String isEmpty() Example
public class EmptyStringExample {
    public static void main(String[] args) {
        String empty = "";
        String spaces = "   ";
        System.out.println(empty.length() + " " + empty.isEmpty());
        System.out.println(spaces.length() + " " + spaces.isEmpty() + " " + spaces.isBlank());
    }
}

// output:
// 0 true
// 3 false true

Get the length without leading and trailing spaces

trim() removes spaces, tabs and line breaks at both ends of the string. Call it before length() when the text comes from user input.

Java String trim() and length() Example
public class TrimLengthExample {
    public static void main(String[] args) {
        String input = "  Java  ";
        System.out.println(input.length());
        System.out.println(input.trim().length());
    }
}

// output:
// 8
// 4

Count characters with codePointCount()

length() counts UTF-16 code units, and most emoji take two of them. codePointCount() counts Unicode code points, so the smiling face emoji 😀, written in the code as \uD83D\uDE00, counts as one.

Java String codePointCount() Example
public class CodePointExample {
    public static void main(String[] args) {
        String text = "Hi \uD83D\uDE00";
        System.out.println(text.length());
        System.out.println(text.codePointCount(0, text.length()));
    }
}

// output:
// 5
// 4

See also

Updated: