Java String charAt() method in Java

charAt() method is used to get the element present at the specified index in the string. In this post, we will be looking at the charAt() method of the String class in Java.

  • Method declaration – public char charAt(int index)
  • What does it do? It will accept an index in the function’s argument and get the character present at the specified index. Remember, the indexing here will be 0-index, which means if you want to get the character at the 2nd position in the string, you’ll have to pass 1 as an index in the argument.
  • What does the charAt() method return? It will return a char value present at the specified index. If the index is greater than or equal to the string size, it will give us the StringIndexOutOfBoundsException.

Direct access in Strings doesn’t work as it does in arrays, as shown below –

String str = "hello";
char c = str[1];    // this will give you error, because you can't access 
                   // strings in java directly
So, now, how to access the characters of a String in Java?

Java provides us with a charAt() method. We have to pass the index, which will get us the character present at the specified index.

public class Codekru {

	public static void main(String[] args) {
		String str = "hello";
		System.out.println("2nd character in the string is: " + str.charAt(1));
	}
}

Output –

2nd character in the string is: e

Again, we have passed 1 in the argument to access the 2nd character of the string using 0-based indexing.

Time complexity of the charAt() method

Java stores the string as an array internally, and we can access any character of a string in a constant time. So, the time complexity of the charAt() method is O(1).

The What If scenarios

Q – What if you passed a negative index or an index greater than or equal to string length?

In both of the cases, you will get StringIndexOutOfBoundsException, as shown below.

public class Codekru {

	public static void main(String[] args) {
		String str = "hello";
		System.out.println(str.charAt(-1));
	}
}

Output –

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Q – What if you want to fetch the last element of the String?

If you don’t want to count the number of characters in a string and then pass it into the index to get the last element, then we’ve got your back. You can use str.length() to get the actual size of the string and pass str.length() -1 into the function’s argument as an index to get the last element of the String.

public class Codekru {

	public static void main(String[] args) {
		String str = "hello codekru";
		System.out.println("last character in the string is: " + str.charAt(str.length() - 1));
	}
}

Output –

last character in the string is: u
Q – What if we use the charAt() method on a null string?

We will get a NullPointerException.

public class Codekru {

	public static void main(String[] args) {
		String str = null;
		System.out.println(str.charAt(1));
	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.charAt(int)" because "str" is null

If you want to learn more about Strings and its methods, we recommend you read this article.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

References – https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int)

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *