logo

Chaîne Java charAt()

Le Classe de chaîne Java charAt() la méthode renvoie une valeur de caractère au numéro d'index donné .

Le numéro d'index commence à 0 et va jusqu'à n-1, où n est la longueur de la chaîne. Il revient StringIndexOutOfBoundsException, si le numéro d'index donné est supérieur ou égal à cette longueur de chaîne ou à un nombre négatif.

Syntaxe

 public char charAt(int index) 

La méthode accepte indice comme paramètre. L'index de départ est 0. Il renvoie un caractère à une position d'index spécifique dans une chaîne. Ça jette StringIndexOutOfBoundsException si l'index est une valeur négative ou supérieure à cette longueur de chaîne.

Spécifié par

CharSequence interface, située dans le package java.lang.

Implémentation interne

 public char charAt(int index) { if ((index = value.length)) { throw new StringIndexOutOfBoundsException(index); } return value[index]; } 

Exemples de méthodes Java String charAt()

Voyons le programme Java lié à la chaîne dans lequel nous utiliserons la méthode charAt() qui effectue une opération sur la chaîne donnée.

Nom de fichier: CharAtExample.java

 public class CharAtExample{ public static void main(String args[]){ String name='javatpoint'; char ch=name.charAt(4);//returns the char value at the 4th index System.out.println(ch); }} 
Testez-le maintenant

Sortir:

 t 

Voyons l'exemple de la méthode charAt() où nous transmettons une valeur d'index plus grande. Dans un tel cas, il lève StringIndexOutOfBoundsException au moment de l'exécution.

Nom de fichier: CharAtExample.java

 public class CharAtExample{ public static void main(String args[]){ String name='javatpoint'; char ch=name.charAt(10);//returns the char value at the 10th index System.out.println(ch); }} 

Sortir:

 Exception in thread 'main' java.lang.StringIndexOutOfBoundsException: String index out of range: 10 at java.lang.String.charAt(String.java:658) at CharAtExample.main(CharAtExample.java:4) 

Accès au premier et au dernier caractère à l'aide de la méthode charAt()

Voyons un exemple simple où nous accédons au premier et au dernier caractère de la chaîne fournie.

Nom de fichier: CharAtExample3.java

 public class CharAtExample3 { public static void main(String[] args) { String str = 'Welcome to Javatpoint portal'; int strLength = str.length(); // Fetching first character System.out.println('Character at 0 index is: '+ str.charAt(0)); // The last Character is present at the string length-1 index System.out.println('Character at last index is: '+ str.charAt(strLength-1)); } } 

Sortir:

 Character at 0 index is: W Character at last index is: l 

Imprimer les caractères présentés à des positions impaires à l'aide de la méthode charAt()

Voyons un exemple où nous accédons à tous les éléments présents à un index impair.

Nom de fichier: CharAtExample4.java

 public class CharAtExample4 { public static void main(String[] args) { String str = &apos;Welcome to Javatpoint portal&apos;; for (int i=0; i<=str.length()-1; i++) { if(i%2!="0)" system.out.println('char at '+i+' place '+str.charat(i)); } < pre> <p> <strong>Output:</strong> </p> <pre> Char at 1 place e Char at 3 place c Char at 5 place m Char at 7 place Char at 9 place o Char at 11 place J Char at 13 place v Char at 15 place t Char at 17 place o Char at 19 place n Char at 21 place Char at 23 place o Char at 25 place t Char at 27 place l </pre> <p>The position such as 7 and 21 denotes the space.</p> <h3>Counting Frequency of a character in a String by Using the charAt() Method</h3> <p>Let&apos;s see an example in which we are counting frequency of a character in the given string.</p> <p> <strong>FileName:</strong> CharAtExample5.java</p> <pre> public class CharAtExample5 { public static void main(String[] args) { String str = &apos;Welcome to Javatpoint portal&apos;; int count = 0; for (int i=0; i<=str.length()-1; i++) { if(str.charat(i)="=" 't') count++; } system.out.println('frequency of t is: '+count); < pre> <p> <strong>Output:</strong> </p> <pre> Frequency of t is: 4 </pre> <h3>Counting the Number of Vowels in a String by Using the chatAt() Method</h3> <p>Let&apos;s see an example where we are counting the number of vowels present in a string with the help of the charAt() method.</p> <p> <strong>FileName:</strong> CharAtExample6.java</p> <pre> // import statement import java.util.*; public class CharAtExample6 { ArrayList al; // constructor for creating and // assigning values to the ArrayList al CharAtExample6() { al = new ArrayList(); al.add(&apos;A&apos;); al.add(&apos;E&apos;); al.add(&apos;a&apos;); al.add(&apos;e&apos;); al.add(&apos;I&apos;); al.add(&apos;O&apos;); al.add(&apos;i&apos;); al.add(&apos;o&apos;); al.add(&apos;U&apos;); al.add(&apos;u&apos;); } // a method that checks whether the character c is a vowel or not private boolean isVowel(char c) { for(int i = 0; i <al.size(); 1 i++) { if(c="=" al.get(i)) return true; } false; a method that calculates vowels in the string s public int countvowels(string s) countvowel="0;" store total number of size="s.length();" for(int j="0;" < size; j++) char c="s.charAt(j);" if(isvowel(c)) vowel found! increase count by + 1; countvowel; main static void main(string argvs[]) creating an object class charatexample6 obj="new" charatexample6(); str="Javatpoint is a great site for learning Java." ; noofvowel="obj.countVowels(str);" system.out.println('string: ' str); system.out.println('total are: '+ '
'); noofvowel); pre> <p> <strong>Output:</strong> </p> <pre> String: Javatpoint is a great site for learning Java. Total number of vowels in the string are: 16 String: One apple in a day keeps doctor away. Total number of vowels in the string are: 13 </pre> <hr></al.size();></pre></=str.length()-1;></pre></=str.length()-1;>

Les positions telles que 7 et 21 désignent l'espace.

Compter la fréquence d'un caractère dans une chaîne à l'aide de la méthode charAt()

Voyons un exemple dans lequel nous comptons la fréquence d'un caractère dans la chaîne donnée.

Nom de fichier: CharAtExample5.java

 public class CharAtExample5 { public static void main(String[] args) { String str = &apos;Welcome to Javatpoint portal&apos;; int count = 0; for (int i=0; i<=str.length()-1; i++) { if(str.charat(i)="=" \'t\') count++; } system.out.println(\'frequency of t is: \'+count); < pre> <p> <strong>Output:</strong> </p> <pre> Frequency of t is: 4 </pre> <h3>Counting the Number of Vowels in a String by Using the chatAt() Method</h3> <p>Let&apos;s see an example where we are counting the number of vowels present in a string with the help of the charAt() method.</p> <p> <strong>FileName:</strong> CharAtExample6.java</p> <pre> // import statement import java.util.*; public class CharAtExample6 { ArrayList al; // constructor for creating and // assigning values to the ArrayList al CharAtExample6() { al = new ArrayList(); al.add(&apos;A&apos;); al.add(&apos;E&apos;); al.add(&apos;a&apos;); al.add(&apos;e&apos;); al.add(&apos;I&apos;); al.add(&apos;O&apos;); al.add(&apos;i&apos;); al.add(&apos;o&apos;); al.add(&apos;U&apos;); al.add(&apos;u&apos;); } // a method that checks whether the character c is a vowel or not private boolean isVowel(char c) { for(int i = 0; i <al.size(); 1 i++) { if(c="=" al.get(i)) return true; } false; a method that calculates vowels in the string s public int countvowels(string s) countvowel="0;" store total number of size="s.length();" for(int j="0;" < size; j++) char c="s.charAt(j);" if(isvowel(c)) vowel found! increase count by + 1; countvowel; main static void main(string argvs[]) creating an object class charatexample6 obj="new" charatexample6(); str="Javatpoint is a great site for learning Java." ; noofvowel="obj.countVowels(str);" system.out.println(\'string: \' str); system.out.println(\'total are: \'+ \'
\'); noofvowel); pre> <p> <strong>Output:</strong> </p> <pre> String: Javatpoint is a great site for learning Java. Total number of vowels in the string are: 16 String: One apple in a day keeps doctor away. Total number of vowels in the string are: 13 </pre> <hr></al.size();></pre></=str.length()-1;>

Compter le nombre de voyelles dans une chaîne à l'aide de la méthode chatAt()

Voyons un exemple où nous comptons le nombre de voyelles présentes dans une chaîne à l'aide de la méthode charAt().

Nom de fichier: CharAtExample6.java

 // import statement import java.util.*; public class CharAtExample6 { ArrayList al; // constructor for creating and // assigning values to the ArrayList al CharAtExample6() { al = new ArrayList(); al.add(&apos;A&apos;); al.add(&apos;E&apos;); al.add(&apos;a&apos;); al.add(&apos;e&apos;); al.add(&apos;I&apos;); al.add(&apos;O&apos;); al.add(&apos;i&apos;); al.add(&apos;o&apos;); al.add(&apos;U&apos;); al.add(&apos;u&apos;); } // a method that checks whether the character c is a vowel or not private boolean isVowel(char c) { for(int i = 0; i <al.size(); 1 i++) { if(c="=" al.get(i)) return true; } false; a method that calculates vowels in the string s public int countvowels(string s) countvowel="0;" store total number of size="s.length();" for(int j="0;" < size; j++) char c="s.charAt(j);" if(isvowel(c)) vowel found! increase count by + 1; countvowel; main static void main(string argvs[]) creating an object class charatexample6 obj="new" charatexample6(); str="Javatpoint is a great site for learning Java." ; noofvowel="obj.countVowels(str);" system.out.println(\'string: \' str); system.out.println(\'total are: \'+ \'
\'); noofvowel); pre> <p> <strong>Output:</strong> </p> <pre> String: Javatpoint is a great site for learning Java. Total number of vowels in the string are: 16 String: One apple in a day keeps doctor away. Total number of vowels in the string are: 13 </pre> <hr></al.size();>