English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Java

Contrôle de flux Java

Java Tableau

Java Programmation orientée objet (I)

Java Programmation orientée objet (II)

Java Programmation orientée objet (III)

Gestion des exceptions Java

Java Liste (List)

Java Queue (file d'attente)

Java Map collectif

Java Set collectif

Entrée/sortie Java (I/O)

Reader Java/Writer

Autres sujets Java

Utilisation et exemples de la méthode length() de String Java

Java String (string) methods

La méthode length() de String Java retourne la longueur de la chaîne.

La syntaxe de la méthode length() est :

string.length()

length() parameters

  • The length() method does not take any parameters.

The return value of length()

  • The length() method returns the length of the given string.

The length is equal to the number of char values (code units) in the string.

Example: Java string length()

class Main {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = "";
        System.out.println(str1.length());  // 4
        System.out.println(str2.length());  // 0
        System.out.println("Java".length());  // 4
        System.out.println("Java\n".length()); // 5
        System.out.println("Learn Java".length()); // 10
    }
}

In the above program, the length of the "Java\n" string is5Instead of6This is because '\n' in Java is a single character (newline).

Java String (string) methods