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

Tutoriel de base Java

Java contrôle de flux

Java tableau

Java orienté objet (I)

Java orienté objet (II)

Java orienté objet (III)

Gestion des exceptions Java

Java List

Java Queue (file d'attente)

Java Map collection

Java Set collection

Java entrée/sortie (I/O)

Java Reader/Writer

Autres sujets Java

Java programme itératif parcourant chaque caractère de la chaîne

Java example complete list

Dans ce tutoriel, nous allons apprendre à parcourir chaque caractère de la chaîne.

Pour comprendre cet exemple, vous devriez comprendre ce qui suitJava programmationSujet :

Example1:parcourir chaque caractère de la chaîne avec une boucle for

class Main {
  public static void main(String[] args) {
    //Create a string
    String name = "w3codebox";
    System.out.println( name + "The characters in ");
    //parcourir chaque élément
    for(int i = 0; i<name.length(); i++) {
      //Access each character
      char a = name.charAt(i);
      System.out.print(a + "");
    }
  }
}

Output result

w3The characters in codebox are:
n,h,o,o,o,

Dans l'exemple précédent, nous avons utiliséboucle forto access each element of the string. Here, we used the charAt() method to access each character of the string.

Example2:Use for-each loop traverses each character of the string

class Main {
  public static void main(String[] args) {
    //Create a string
    String name = "w3codebox";
    System.out.println( name + "The characters in ");
    //Use for-each loop traverses each element
    for(char c : name.toCharArray()) {
      //Access each character
      System.out.print(c + "");
    }
  }
}

Output result

w3The characters in codebox are:
n,h,o,o,o,

In the above example, we use toCharArray() to convert the string to a char array. Then, we use for-each loop Access each element of the char array.

Java example complete list