/* Fichier Ex1String : exemple sur les chaînes de caractères * */ public class Ex1String { public static void main(String[] args) { String codePermanent = "TREP23119006" ; // Tremblay Pierre né le 23 novembre 1990 System.out.printf("Le marqueur pour indices: 012345678901234567890123456789\n"); System.out.printf("La chaîne telle quelle : %s\n", codePermanent); System.out.printf("La chaîne en MAJUSCULEs : %s\n", codePermanent.toUpperCase()); System.out.printf("La chaîne en minuscules : %s\n", codePermanent.toLowerCase()); System.out.printf("La longueur : %d caractères\n\n", codePermanent.length()); System.out.printf("Le caractère à l'indice 3 : %c\n", codePermanent.charAt(3)); System.out.printf("La sous-chaîne codePermanent.substring(0, 3) vaut %s\n", codePermanent.substring(0, 3)); System.out.printf("La sous-chaîne codePermanent.substring(8) vaut: %s\n", codePermanent.substring(8)); int anneeNaissance = 1900 + Integer.parseInt( codePermanent.substring(8,10)); System.out.printf("L'année de naissance : %d\n", anneeNaissance); int k = codePermanent.indexOf('R'); System.out.printf("On trouve la lettre R à l'indice %d\n", k); k = codePermanent.indexOf('Z'); if (k == -1) System.out.printf("On ne trouve pas la lettre Z : indice vaut %d\n", k); k = codePermanent.indexOf("P2311"); if ( k >= 0) System.out.printf("On trouve la sous-chaîne P2311 à l'indice %d\n", k); k = codePermanent.indexOf("777"); if (k == -1) System.out.printf("On ne trouve pas la sous-chaîne 777 et l'indice vaut %d\n", k); } } /* --------------------Configuration: -------------------- Le marqueur pour indices: 012345678901234567890123456789 La chaîne telle quelle : TREP23119006 La chaîne en MAJUSCULEs : TREP23119006 La chaîne en minuscules : trep23119006 La longueur : 12 caractères Le caractère à l'indice 3 : P La sous-chaîne codePermanent.substring(0, 3) vaut TRE La sous-chaîne codePermanent.substring(8) vaut: 9006 L'année de naissance : 1990 On trouve la lettre R à l'indice 1 On ne trouve pas la lettre Z : indice vaut -1 On trouve la sous-chaîne P2311 à l'indice 3 On ne trouve pas la sous-chaîne 777 et l'indice vaut -1 Process completed. */