What is the use of charAt () method?

The Java String charAt(int index) method returns the character at the specified index in a string. The index value that we pass in this method should be between 0 and (length of string-1). For example: s.charAt(0) would return the first character 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 zero or greater than or equal to the length of the string (index<0|| index>=length()).

Java String charAt() Method example

Lets take an example to understand 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 class CharAtExample {
   public static void main(String args[]) {
	String str = "Welcome to string handling tutorial";
	//This will return the first char of the string
	char ch1 = str.charAt(0);
		
	//This will return the 6th char of the string
	char ch2 = str.charAt(5);
		
	//This will return the 12th char of the string
	char ch3 = str.charAt(11);
		
	//This will return the 21st char of the string
	char ch4 = str.charAt(20);
		
	System.out.println("Character at 0 index is: "+ch1);
	System.out.println("Character at 5th index is: "+ch2);
	System.out.println("Character at 11th index is: "+ch3);
	System.out.println("Character at 20th index is: "+ch4);
   }
}

Output:

Character at 0 index is: W
Character at 5th index is: m
Character at 11th index is: s
Character at 20th index is: n

IndexOutOfBoundsException while using charAt() method

When we 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 index 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(-1);
	System.out.println(ch);
   }
}

Output:

What is the use of charAt () method?

Java 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 string – 1 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()-1; i++) {
		System.out.println(str.charAt(i));
	}
   }
}

Output:

B
e
g
i
n
n
e
r
s
B
o
o
k

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

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

This JavaScript tutorial explains how to use the string method called charAt() with syntax and examples.

Description

In JavaScript, charAt() is a string method that is used to retrieve a character at a specific position in a string. Because the charAt() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the charAt() method is:

string.charAt([position]);

Parameters or Arguments

positionOptional. It is the position of the character in string that you wish to retrieve. The first position in the string is 0. If this parameter is not provided, the charAt() method will use 0 as the default.

Returns

The charAt() method returns a string representing a character at a specific position in a string.

The position must be between 0 and string.length-1. If the position is out of bounds, the charAt() method will return an empty string.

Note

  • The charAt() method does not change the value of the original string.

Example

Let's take a look at an example of how to use the charAt() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charAt(0));
console.log(totn_string.charAt(1));
console.log(totn_string.charAt(2));
console.log(totn_string.charAt(3));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the charAt() method of the totn_string variable to return a character at a specific position.

We have written the output of the charAt() method to the web browser console log, for demonstration purposes, to show what the charAt() method returns.

The following will be output to the web browser console log:

T
e
c
h

As you can see, the charAt() method returned a single character from the string in all four cases. The first call to the charAt() method returned "T" which is the character at position 0 of the string. The second call returned "e" which is the character at position 1. The third call returned "c" which is the character at position 2. The fourth call returned "h" which is the character at position 3.

No Parameter is Provided

Next, let's see what happens if you don't provide a position parameter to the charAt() method.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charAt());

The following will be output to the web browser console log:

T

When no position parameter is provided, the charAt() method will use 0 as the value of the position parameter. In this example, the charAt() method returned the first character in the string (which is position 0) when no parameter was passed to the method.

Parameter is Out of Bounds

Finally, let's see what happens if the charAt() method is passed a position value that is out of bounds.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charAt(85));

The following will be output to the web browser console log:

Because the first position in the string is 0, the position parameter must be a value between 0 and string.length-1. If the position parameter is out of bounds and does not fall in this range, the charAt() method will return an empty string.

Since 85 is a position that is out of bounds for the string 'TechOnTheNet', the charAt() method returned an empty string in the above example.

What is the use of charAt () method CSS?

The charAt() method returns the character at a specified index (position) in a string.

When can you use charAt in Java?

The charAt() method in Java returns the char value of a character in a string at a given or specified index.

What charAt means?

Description. charAt() is a method that returns the character from the specified index. Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string, called stringName, is stringName.

What is meant by charAt i )

charAt(i) will return a character and string. charAt(i)- '0' will return the actual integer value. For e.g. string="12345",this method will return an integer array [1,2,3,4,5].