Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

String charAt()

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In this blog, we will learn more about the java string charAt() function with the help of examples. The charAt() method of the Java String class returns a char value at the specified index number. The index number starts at 0 and continues up to n-1, where n is the string length. If the specified index number is larger than or equal to this string length or a negative integer, it throws StringIndexOutOfBoundsException.

String charAt()

String charAt() is a method found in java.lang.String in class. The syntax to use the method is given below.

Syntax:

public char charAt(int index) 

The index argument is accepted by the procedure. The initial index is zero. It returns a character from a string at a given index position. If the index is negative or longer than this string length, it raises StringIndexOutOfBoundsException.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example

The charAt() function returns the character in a string at the given index. The first character has an index of 0, and the second has an index of 1, and so on.

Program:

public class Stringoperation4  
{  
    public static void main(String args[])  
    {  
        String str="CodingNinjas";    
        System.out.println(str.charAt(0));//C    
        System.out.println(str.charAt(6));//N    
    }  
}

Output:

C
N

Let's look at using the charAt() function with a higher index value. At runtime, it throws a StringIndexOutOfBoundsException in this situation.

Program:

public class charExample{  
    public static void main(String args[]){  
        String str="codingninjas";  
        char ch=str.charAt(20);//returns char at the 10th index  
        System.out.println(ch);  
    }
}  

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 20
at java.lang.String.charAt(String.java:658)
at charExample.main(charExample.java:4)

Frequently Asked Questions

What is a string class in Java?

The java.lang.String class in Java has a multitude of built-in methods for manipulating strings. We may execute actions on String objects using these methods, such as cutting, concatenating, converting, comparing, and replacing strings.

What do you mean by strings being immutable?

String objects are immutable in Java, meaning they can't be updated or modified. A string object's data or state cannot be changed after being created.

Which type of indexing is used in the charAt() method?

0-based indexing is used while using the charAt() method. The first character has an index of 0; the second has an index of 1, and so on.

Conclusion

In this article, we have extensively discussed the string class method charAt() of the java programming language with various examples.

We hope this blog has helped you enhance your charAt() method knowledge. If you would like to learn more, check out our articles on Understanding StringsStrings Operations, and String Interview Questions In Java.

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Topics covered
1.
Introduction
2.
String charAt()
3.
Example
4.
Frequently Asked Questions
4.1.
What is a string class in Java?
4.2.
What do you mean by strings being immutable?
4.3.
Which type of indexing is used in the charAt() method?
5.
Conclusion