Java - String charAt() Method



The Java String charAt() method is used to retrieve the character at the specified index. The indexes refer to the character position in the sequence. The first char value represents the 0th index, and the next char value represents the 1st index, and so on. The indexes start with 0 index and end with the length()-1 index.

A string is an object that holds the sequence of the characters. The java.lang.string class represents the string in Java. The strings are constant in Java their values cannot be changed after they are created.

The charAt() method accepts a parameter as an integer that holds the value of the index. It throws an exception if the index value is negative or greater than the sequence length.

Syntax

Following is the syntax of the Java String charAt() method −

public char charAt(int index)

Parameters

  • index − This is the index of the char value.

Return Value

This method returns the char value at the specified index of this string. The first char value is at index 0.

Example

If the index value is non-negative and less than the string length, the charAt() method returns the character value at the specified index.

In the following program, we create a string literal with the value “This is tutorialspoint”. Then using the charAt() method, we are retrieving the characters at the specified indexes 0, 4, and 17.

package com.tutorialspoint;
import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      
      //create an string literal
      String str = "This is tutorialspoint";
      System.out.print("The string is: " + str);
      
      //initialize the index values
      int index1 = 0;
      int index2 = 4;
      int index3 = 17;
      System.out.println("The index values are: " + index1 + ", " + index2 + " and " + index3);
      //using the charAt() method
      System.out.println("The character value at the " + index1 + " index is: " + str.charAt(index1));
      System.out.println("The character value at the " + index2 + " index is: " + str.charAt(index2));
      System.out.println("The character value at the " + index3 + " index is: " + str.charAt(index3));
   }
}

Output

On executing the above program, it will produce the following result −

The string is: This is tutorialspoint
The index values are: 0, 4 and 17
The character value at the 0 index is: T
The character value at the 4 index is:  
The character value at the 17 index is: p

Example

If the index value is negative, this method throws an IndexOutOfBoundsException.

In this program, we are creating a string literal with the value “Tutorials Point”. Then, using the charAt() method, we are trying to retrieve the character value at the specified index -1.

package com.tutorialspoint;
import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      try {
         
         // create a string literal
         String str = "Tutorials Point";
         System.out.println("The string value is: " + str);
         
         //initialize the index value
         int index = -1;
         System.out.println("The index value is: " + index);
         
         // use charAt() method
         System.out.println("The character value at the specifed index " + index + " is: " + str.charAt(index));
      } catch(Exception e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The string value is: Tutorials Point
The index value is: -1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
	at java.base/java.lang.String.charAt(String.java:1515)
	at com.tutorialspoint.Demo.main(Demo.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Example

If the index value is greater than the string length, the CharAt() method throws an IndexOutOfBoundsException.

In the following example, we are creating an object of the string class with the value “Welcome to Tutorials Point”. Then, using the charAt() method we are trying to retrieve the character at the specified index whose value is greater than the string length.

package com.tutorialspoint;
import java.lang.*;
public class Test {
   public static void main(String[] args) {
      try {
         
         // create an object of the string
         String str = new String("Welcome to Tutorials Point");
         System.out.println("The string value is: " + str);
         
         //initialize the index value
         int index = 50;
         System.out.println("The index value is: " + index);
         
         // use charAt() method
         System.out.println("The character value at the specifed index " + index + " is: " + str.charAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

The above program, produces the following results −

The string value is: Welcome to Tutorials Point
The index value is: 50
java.lang.StringIndexOutOfBoundsException: String index out of range: 50
	at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
	at java.base/java.lang.String.charAt(String.java:1515)
	at com.tutorialspoint.Test.main(Test.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 50

Example

Using the for loop along with the charAt() method, we can retrieve all the characters at the specified index.

In this program, we are instantiating the string class with the value “TutorialsPoint”. Then, in the loop will iterate through the given sequence. Using the charAt() method, we are trying to retrieve all the characters at the specified indexes.

package com.tutorialspoint;
import java.lang.*;
public class ClassDemo {
   public static void main(String[] args) {
      
      // create an object of the String
      String str = new String("TutorialsPoint");
      System.out.println("The given string is: " + str);
      System.out.print("The characters of the given string are: ");
      
      // using for loop along with the charAt() method
      for (int i = 0; i < str.length(); i++) {
         System.out.print(str.charAt(i) + " ");
      }
   }
}

Output

The given string is: TutorialsPoint
The characters of the given string are: T u t o r i a l s P o i n t
java_lang_string.htm
Advertisements