Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
The charAt() in Java is a method, used particularly to return the character at the specified index in a string. In this article we will understand this topic in detail. Following pointers will be covered in this article,
For charAt() method, the index value passed must be between 0 and (length of string – 1). In case the index value is greater than, equal to, or a negative number, a StringIndexOutOfBoundsException is returned.
Signature
public char charAt(int index)
Parameter
index: The index of the character to be returned
Return
The character at the specified position is returned.
Exception
StringIndexOutOfBoundException: Returned if the value of the index is negative, greater than, or equal to the length of the string.
Moving on with this charAt in Java article
Example
public class Main { public static void main(String args[]) { String str = "We must save the planet from climate change"; //This returns the first character of the string char c1 = str.charAt(0); char c2 = str.charAt(5); char c3 = str.charAt(9); char c4 = str.charAt(15); System.out.println("Character at 0 index : "+c1); System.out.println("Character at 5th index : "+c2); System.out.println("Character at 9th index : "+c3); System.out.println("Character at 15th index : "+c4); } }
Output
Character at 0 index is: W
Character at 5th index is: s
Character at 11th index is: a
Character at 20th index is: e
Moving on with this charAt in Java article
On passing a negative index, or index greater than length() – 1 , then a StringIndexOutOfBoundsException is thrown.
In the following example, a negative index is passed:
public class Main { public static void main(String args[]) { String str = "ClimateChange"; //negative index char c = str.charAt(-1); System.out.println(c); } }
Output
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.base/java.lang.StringLatin1.charAt(tringLatin1.java:44)
at java.base/java.lang.String.charAt(String.java:692)
at Main.main(Main.java:5)
Command exited with non-zero status 1
The code terminates with an exception.
Moving on with this charAt in Java article
The usage of for loop from 0 to the length of string() -1 is made, to print all characters of a string.
</p> public class Main { public static void main(String args[]) { String s = "ClimateChange"; for(int i=0; i<=s.length()-1; i++) { System.out.println(s.charAt(i)); } } } <p style="text-align: justify;">
Output
C
l
i
m
a
t
e
C
h
a
n
g
e
Moving on with this charAt in Java article
public class Main { public static void main(String[] args) { String s = "ClimateChangeIsReal"; int count = 0; for (int i=0; i<=s.length()-1; i++) { if(s.charAt(i) == 'C'){ count++; } } System.out.println("Frequency of C is: "+count); } }
The frequency of a character can be determined by using charAt():
Output
Frequency of C is: 2
Moving on with this charAt in Java article
The first and the last character of a string can be printed using charAt():
public class Main { public static void main(String[] args) { String s = "Climate Change Is Real"; int strLength = s.length(); //first character System.out.println("Character at 0 index : "+ s.charAt(0)); //Fetching last Character present at the string length-1 index System.out.println("Character at last index : "+ s.charAt(strLength-1)); } }
Output
Character at 0 index : C
Character at last index : l
The charAt() method provides the user with innumerous ways to access the elements at any specified index, as long as the index falls within an appropriate range.
Thus we have come to an end of this article on ‘charAt in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co