Skip to content Skip to sidebar Skip to footer

How to Read Each Character of a String in Java

The Java String charAt(int index) method returns the character at the specified index in a string. The index value that we laissez passer in this method should be betwixt 0 and (length of string-one). For example: s.charAt(0) would return the first character of the cord represented past example southward. Coffee String charAt method throws IndexOutOfBoundsException, if the alphabetize value passed in the charAt() method is less than zero or greater than or equal to the length of the cord (index<0|| alphabetize>=length()).

Java String charAt() Method example

Lets take an case to sympathize the use of charAt() method. In this example we have a string and we are press the 1st, 6th, 12th and 21st character of the string using charAt() method.

public class CharAtExample {    public static void master(String args[]) { 	Cord str = "Welcome to cord handling tutorial"; 	//This will return the first char of the string 	char ch1 = str.charAt(0); 		 	//This will render the 6th char of the cord 	char ch2 = str.charAt(5); 		 	//This volition render the 12th char of the string 	char ch3 = str.charAt(11); 		 	//This volition return the 21st char of the cord 	char ch4 = str.charAt(20); 		 	Organisation.out.println("Graphic symbol at 0 index is: "+ch1); 	System.out.println("Graphic symbol at 5th index is: "+ch2); 	System.out.println("Graphic symbol at 11th index is: "+ch3); 	Organization.out.println("Graphic symbol at 20th index is: "+ch4);    } }

Output:

Character at 0 index is: W Character at fifth alphabetize is: m Character at 11th index is: southward Character at 20th alphabetize is: north

IndexOutOfBoundsException while using charAt() method

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

public class JavaExample {    public static void main(String args[]) { 	String str = "BeginnersBook"; 	//negative index, method would throw exception 	char ch = str.charAt(-one); 	Organization.out.println(ch);    } }          

Output:
Java String charAt method

Coffee String charAt() example to print all characters of string

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

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

Output:

B e g i northward n due east r s B o o k

Java String charAt() instance to count the occurrence of a graphic symbol

In this case, nosotros will apply the charAt() method to count the occurrence of a particular character in the given string. Hither we have a cord and we are counting the occurrence of character 'B' in the cord.

public form JavaExample {    public static void main(String[] args) {           String 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

Cord charAt() javadoc

currywhicent.blogspot.com

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

Post a Comment for "How to Read Each Character of a String in Java"