AppDividend
Latest Code Tutorials

charAt() In Java Example | Java String charAt() Method

0

charAt() In Java Example | Java String charAt() Method is today’s topic. Java charAt() is a method of string which helps us find the position of any character in the string. The index that we have to put should be in between 0 to (length_of_string – 1). For example, s.charAt(0) would return the first character of a string represented by instance s. Java String charAt method throws an IndexOutOfBoundsException if an index value passed in a charAt() method is less than 0 or greater than or equal to a length of the string (index<0|| index>=length()).

charAt() In Java Example

The java.lang.String.charAt() method returns a char value at a specified index. An index ranges from 0 to length() – 1. The first char value of a sequence is at index 0, the next at index 1, and so on, as for array indexing.

Syntax:

character = string_name.charAt(index_position)

So, there is one parameter in charAt() which is the index of string.

Various Example of charAt()

Find character of at any given index:

In this method, we are going to find the position of any character in the string.

class CharAt {
	public static void main(String args[]) {
		String str = "Welcome to AppDividend";
		// This will return the first char of the string
		char ch1 = str.charAt(0);
		// This will return the 7th char of the string
		char ch2 = str.charAt(6);
		// This will return the 12th char of the string
		char ch3 = str.charAt(11);
		System.out.println("Character at 0 index is: " + ch1);
		System.out.println("Character at 7th index is: " + ch2);
		System.out.println("Character at 12th index is: " + ch3);
	}
}

See the following output.

 

charAt() In Java

IndexOutOfBoundsException

This situation happens when we enter the wrong index position of any string. 

Suppose we have a string of length 5, so if we write charAt(10) then there is no such character at that position. So we will get IndexOutOfBoundExpection.

See the following program.

class CharAt {
	public static void main(String args[]) {
		String str = "Welcome to AppDividend";
		// here we are giving one index position which is not
		// present actuallu
		char ch = str.charAt(150);
		System.out.println(ch);
	}
}

See the output.

 

Java String charAt() Method

Print all characters of a string using charAt()

In this example, we print all the characters of a given string using charAt().

class CharAt {
	public static void main(String args[]) {
		String str = "Welcome to Appdividend";
		System.out.println("All the characters of the string are: ");
		for (int i = 0; i <= str.length() - 1; i++) {
			System.out.print(str.charAt(i) + " ");
		}
	}
}

See the output.

 

Print all characters of a string using charAt()

Count the occurrence of a character

In this example, we will use a charAt() method to count the occurrence of the particular character in the given string. Here we have the string, and we are counting the occurrence of character ‘B’ in the string.

public class 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++;
			}
		}
		System.out.println("Char 'B' occurred " + counter + " times in the string");
	}
}

Finally, charAt() In Java Example | Java String charAt() Method is over.

Recommended Posts

Transient Keyword In Java

Java Inner Class Tutorial

Serialization and Deserialization in Java Tutorial

Java File Class Tutorial

StringBuilder Class in Java Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.