Read Each Character in a String Java

The Java Cord charAt(int alphabetize) method returns the character at the specified index in a string. The index value that nosotros pass in this method should be betwixt 0 and (length of string-1). For instance: south.charAt(0) would return the outset grapheme of the string represented by instance s. Java String charAt method throws IndexOutOfBoundsException, if the index value passed in the charAt() method is less than aught or greater than or equal to the length of the cord (index<0|| index>=length()).

Coffee String charAt() Method example

Lets accept an instance to empathize the use of charAt() method. In this example we have a string and we are printing the 1st, 6th, 12th and 21st character of the string using charAt() method.

public form CharAtExample {    public static void chief(String args[]) { 	String str = "Welcome to cord handling tutorial"; 	//This will return the starting time char of the string 	char ch1 = str.charAt(0); 		 	//This volition return the 6th char of the string 	char ch2 = str.charAt(v); 		 	//This will return the 12th char of the cord 	char ch3 = str.charAt(11); 		 	//This will render the 21st char of the string 	char ch4 = str.charAt(20); 		 	System.out.println("Graphic symbol at 0 index is: "+ch1); 	System.out.println("Graphic symbol at fifth alphabetize is: "+ch2); 	System.out.println("Graphic symbol at 11th alphabetize is: "+ch3); 	System.out.println("Grapheme at 20th index is: "+ch4);    } }

Output:

Character at 0 index is: W Character at fifth index is: m Graphic symbol at 11th index is: south Character at 20th index is: n

IndexOutOfBoundsException while using charAt() method

When nosotros pass negative index or the index which is greater than length()-one then the charAt() method throws IndexOutOfBoundsException. In the following example we are passing negative index in the charAt() method, lets see what we get in the output.

public grade JavaExample {    public static void primary(String args[]) { 	String str = "BeginnersBook"; 	//negative alphabetize, method would throw exception 	char ch = str.charAt(-1); 	Organisation.out.println(ch);    } }          

Output:
Java String charAt method

Coffee String charAt() case to impress all characters of string

To impress all the characters of a string, we are running a for loop from 0 to length of cord – i and displaying the character at each iteration of the loop using the charAt() method.

public class JavaExample {    public static void main(String args[]) { 	String str = "BeginnersBook"; 	for(int i=0; i<=str.length()-ane; i++) { 		System.out.println(str.charAt(i)); 	}    } }

Output:

B e g i n n eastward r south B o o one thousand

Java String charAt() example to count the occurrence of a character

In this case, we will use the charAt() method to count the occurrence of a particular graphic symbol in the given cord. Hither nosotros take a cord and we are counting the occurrence of grapheme 'B' in the string.

public class JavaExample {    public static void primary(String[] args) {           Cord str = "BeginnersBook";                   //initialized the counter to 0         int counter = 0;                    for (int i=0; i<=str.length()-1; i++) {               if(str.charAt(i) == 'B') {              	//increasing the counter value at each occurrence of 'B'                 counter++;               }           }           Organisation.out.println("Char 'B' occurred "+counter+" times in the string");      }   }

Output:
Java String charAt() example

Reference

String charAt() javadoc

grenierginstioniff.blogspot.com

Source: https://beginnersbook.com/2013/12/java-string-charat-method-example/

0 Response to "Read Each Character in a String Java"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel