Java String charAt()

This article will dive through charAt() method in String class in java with its syntax, arguments and usage with examples.
[the_ad id=”651″] Overview
java.lang.String class has a charAt() method which returns a single character at a given index or position in the string.
A string is made up of multiple characters and each character has a numeric index or position starting from 0 as shown below.
charAt() java string
charAt() syntax
charAt() method takes an integer as argument and returns the character at that index or position.
Index of characters in a string ranges from 0 to (length of string – 1). So, the index range of characters in a string of 6 characters will be from 0 to 5.

Thus, charAt(5) will return the character at 5th index or at 6th position in the string etc.

charAt() throws java.lang.IndexOutOfBoundsException if the index supplied to it is outside of this range.
charAt() example
Below is an example program to demonstrate the use of charAt() method to get the first character in java string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class CharAtExample {
public static void main(String[] a) {
String s = "codippa";
// get first character
char c = s.charAt(0);
System.out.println("Character at index 0 is " + c);
// get third character
c = s.charAt(2);
System.out.println("Character at index 2 is " + c);
}
}
public class CharAtExample { public static void main(String[] a) { String s = "codippa"; // get first character char c = s.charAt(0); System.out.println("Character at index 0 is " + c); // get third character c = s.charAt(2); System.out.println("Character at index 2 is " + c); } }
public class CharAtExample {
   public static void main(String[] a) {
      String s = "codippa";
      // get first character
      char c = s.charAt(0);
      System.out.println("Character at index 0 is " + c);
      // get third character
      c = s.charAt(2);
      System.out.println("Character at index 2 is " + c);
   }
}

This prints

Character at index 0 is c
Character at index 3 is i

[the_ad id=”656″] Get last character with charAt()
As stated above, characters in a string have an index from 0 to (length – 1). This means that last character of string has an index of its length – 1.
Thus, to get the last character of a string, we can use charAt() with its argument as (length of string – 1). Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String s = "codippa";
int length = s.length();
char c = s.charAt(length-1);
System.out.println("Last character of string is " + c);
String s = "codippa"; int length = s.length(); char c = s.charAt(length-1); System.out.println("Last character of string is " + c);
String s = "codippa";
int length = s.length();
char c = s.charAt(length-1);
System.out.println("Last character of string is " + c);

which prints

Last character of string is a

Iterating through string
charAt() can be used to iterate over a string using a loop which ranges from 0 to length of string – 1.
In every iteration, supply the current loop variable to charAt() to get the character at that index. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String s = "codippa";
// get length of string
int length = s.length();
// loop over string
for (int i = 0; i <= length - 1; i++) {
// get character at index using charAt()
char c = s.charAt(i);
System.out.println("Character at index " + i + " is " + c);
}
String s = "codippa"; // get length of string int length = s.length(); // loop over string for (int i = 0; i <= length - 1; i++) { // get character at index using charAt() char c = s.charAt(i); System.out.println("Character at index " + i + " is " + c); }
String s = "codippa";
// get length of string
int length = s.length();
// loop over string
for (int i = 0; i <= length - 1; i++) {
   // get character at index using charAt()
   char c = s.charAt(i);
   System.out.println("Character at index " + i + " is " + c);
}

Output is

Character at index 0 is c
Character at index 1 is o
Character at index 2 is d
Character at index 3 is i
Character at index 4 is p
Character at index 5 is p
Character at index 6 is a

Getting odd characters
Suppose you need to get characters at odd positions in a string such as characters at 1, 3, 5 etc., indexes.

Follow below steps
1. Iterate over the string character by character and check if the index of current character is odd.
2. Get the character with charAt(), if the index is odd. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String s = "codippa";
int length = s.length();
for (int index = 0; index <= length - 1; index++) {
// check for odd index
if (index % 2 != 0) {
char c = s.charAt(index);
System.out.println("Character at index " + index + " is " + c);
}
}
String s = "codippa"; int length = s.length(); for (int index = 0; index <= length - 1; index++) { // check for odd index if (index % 2 != 0) { char c = s.charAt(index); System.out.println("Character at index " + index + " is " + c); } }
String s = "codippa";
int length = s.length();
for (int index = 0; index <= length - 1; index++) {
  // check for odd index
  if (index % 2 != 0) {
    char c = s.charAt(index);
    System.out.println("Character at index " + index + " is " + c);
  }
}

which prints the characters at odd positions as below

Character at index 1 is o
Character at index 3 is i
Character at index 5 is p

[the_ad id=”644″] Count occurrence of character
charAt() can also be used to count the occurrence of a character or how many times a character appeared in a string.
Example shown below counts the occurrence of ‘p’ in the string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String s = "codippa";
int length = s.length();
// count of occurrence
int occurrence = 0;
for (int index = 0; index <= length - 1; index++) {
// check if character
if (s.charAt(index)== 'p') {
// increase count
occurrence++;
}
}
System.out.println("Frequency of \'p\' is " + occurrence);
String s = "codippa"; int length = s.length(); // count of occurrence int occurrence = 0; for (int index = 0; index <= length - 1; index++) { // check if character if (s.charAt(index)== 'p') { // increase count occurrence++; } } System.out.println("Frequency of \'p\' is " + occurrence);
String s = "codippa";
int length = s.length();
// count of occurrence
int occurrence = 0;
for (int index = 0; index <= length - 1; index++) {
   // check if character
   if (s.charAt(index)== 'p') {
     // increase count
     occurrence++;
   }
}
System.out.println("Frequency of \'p\' is " + occurrence);

This prints

Frequency of ‘p’ is 2

In this article, we looked at charAt() method in java string class, what is its use, syntax and how to use it to get a character at an index, iterate through a string, get odd characters of a string and count the occurrence of a character in string with example programs.
Hope the article was helpful.