Java: String charAt(int index) method

In this article, we will discuss how to get a specific character from the supplied/passed string content

 

charAt(int index) method: This String method returns the character value at the specified index position (by invoking from string value/content)

 

Method Signature:

public char charAt(int index);

 

Returns:

Returns the char value at the specified index

 

Throws:

String’s charAt() method throws IndexOutOfBoundsException, if index value supplied/passed falls out of range

IndexOutOfBoundsException is thrown, if the input index value is out of range i.e.;

  1. Index position is negative (<0)
  2. Index position is greater than length()-1

 

Examples on charAt() method:

 

Example 1: Extracting or getting character value at specified index position

StringCharAtMethod.java

package in.bench.resources.string.methods;

public class StringCharAtMethod {

	public static void main(String[] args) {

		// sample string to get char at specified index
		String strSample = "BenchResources.Net"; 

		// returns character value at 5th index position
		char charAt1 = strSample.charAt(5); 

		// printing to the console
		System.out.println("The character at 5th index position is: " 
                                                         + charAt1);  


		// returns character value at 15th index position
		char charAt2 = strSample.charAt(15); 

		// printing to the console
		System.out.println("The character at 15th index position is: " 
                                                          + charAt2);
	}
}

Output:

The character at 5th index position is: R
The character at 15th index position is: N

 

Example 2: Exception scenario by specifying index out of range

StringCharAtMethod.java

package in.bench.resources.string.methods;

public class StringCharAtMethod {

	public static void main(String[] args) {

		// sample string to get char at specified index
		String strSample = "BenchResources.Net"; 

		// returns character value at 18th index position
		char charAt1 = strSample.charAt(18); 

		// printing to the console
		System.out.println("The character at 18th index position is: " 
                                                          + charAt1);  
	}
}

Output:

Exception in thread "main" 
java.lang.StringIndexOutOfBoundsException: String index out of range: 18
	at java.lang.String.charAt(String.java:658)
	at in.bench.resources.string.methods.StringCharAtMethod
                           .main(StringCharAtMethod.java:11)

 

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Read Also:

  • Java: String class
  • Java: String Literal and String constant pool concept
  • Java: String is Immutable
  • Java: String comparison
  • Java: String concatenation

 

Happy Coding !!
Happy Learning !!