- String charAt() method
- Signature of charAt()
- Note
- Calling charAt() to extract a char at a particular index in a String.
- Output is :
- Program Analysis
- Exception in calling chatAt() method.
- Output is :
- Program Analysis
- Java String charAt() Method examples (Find Char At a Given Index)
- 1. Introduction
- Java String charAt() Examples
- Syntax:
- 2. Java String charAt() Method examples:
- how to use charat in java
- Output:
- 3. Example 2: IndexOutOfBoundsException when using charAt() method
- Output:
- 4. Example 3: Printing String char by char using charAt() method
- Output:
- 5. Example 4: Printing even number index characters
- Output:
- 6. Example 5: Printing an odd number of index characters
- Output:
- 7. Example 6: Counting number of occurrences of a character
- Output:
- 8. Conclusion
- Labels:
- SHARE:
- About Us
- Java 8 Tutorial
- Java Threads Tutorial
- Kotlin Conversions
- Kotlin Programs
- Java Conversions
- Java String API
- Spring Boot
- $show=Java%20Programs
- $show=Kotlin
- How to handle StringIndexOutOfBoundsException in Java?
- Example
- Output
- Index in an array
- Index in a Sting
- StringIndexOutOfBoundsException
- Example
- Run time exception
- Handling StringIndexOutOfBoundsException exception
- Output
- Java charat unknown source
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
String charAt() method
As per the name of the method goes, charAt() method of String class is used to determine a character at a particular index in a string.
Signature of charAt()
public char charAt(int index)
- Access modifier — charAt() method is a public method,
- Parameter passed — An intindex is passed to this method
- Value returned — This method returns an char value, present at a particular index in the String object.
Note
When calling charAt() method, the first index in a String object is counted as 0.
Calling charAt() to extract a char at a particular index in a String.
Here in the upcoming code, we have initialized a Sring object with a value and we are calling charAt() method to determine the character present at a particular index in this String object.
// Program to find a character at a particular index of a String. class StringCharAt < public static void main(String[] ar) < String str= new String("Keep Smiling"); System.out.println("Original String is "+ str); //Searching for a character at index 0 in String char c = str.charAt(0); System.out.println("character at the index 2 of "+ str + " is " + c); //Searching for a character at index 5 in String c = str.charAt(4); System.out.println("character at the index 4 of "+ str + " is " + c); //Searching for a character at index 5 in String c = str.charAt(7); System.out.println("character at the index 7 of "+ str + " is " + c); >>
Output is :
Original String is Keep Smiling character at the index 2 of Keep Smiling is K character at the index 4 of Keep Smiling is character at the index 7 of Keep Smiling is i
Program Analysis
- In the code, we have initialized a string str with a value Keep Smiling
- Character present at the index 2 is extracted by calling chatAt(2) on the String, returns — K
- Character present at the index 4 is extracted by calling chatAt(4) on the String, is an empty space.
- Character present at the index 7 is extracted by calling chatAt(7) on the String, returns — i
Exception in calling chatAt() method.
Here in the next code, an Exception is raised when we are trying to determine a character at an out-of-range index in the String object..
// Program to find a character at a particular index of a String. class StringCharAt < public static void main(String[] ar) < String str= new String("Mexico"); //Searching a character at index 9. char c = str.charAt(9); System.out.println("Original String is "+ str); System.out.println("Character at the index 9 of "+ str + " is " + c); >>
Output is :
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9 at java.lang.String.charAt(Unknown Source) at StringCharAt.main(StringCharAt.java:11)
Program Analysis
- In this code above, an Exception of type StringIndexOutOfBoundsException is raised when trying to find a character at an out-of-range index, 9 in the String object which is initialized to the value Mexico.
Java String charAt() Method examples (Find Char At a Given Index)
The Java String charAt(int index) method returns the character at the specified index in a string. If you pass index ass 0 then it retuurms fiest character from String.
1. Introduction
In this article, We’ll learn how to find the character at an index with String charAt in java.
The Java String charAt(int index) method returns the character at the specified index in a string. The index value should be between 0 and (length of string-1). For example, s.charAt(0) would return the first character of the string represented by instance s. Java String charAt method throws IndexOutOfBoundsException if the index value passed in the charAt() method is a negative number or less than zero or greater than or equal to the length of the string (index<0|| index>=length()). IndexOutOfBoundsException means index if out of its range and it occurs at runtime.
Java String charAt() Examples
Syntax:
We will show the example programs on the following use cases.
1: Finding first, mid and last index of the given string.
2: IndexOutOfBoundsException when using charAt() method.
3: Printing String char by char using charAt() method.
4: Printing even numbers of index characters.
5: Printing odd number index characters
6: Counting number of occurrences of a character
2. Java String charAt() Method examples:
how to use charat in java
package examples.java.w3schools.string; public class StringCharAtExample < public static void main(String[] args) < String input = "michael jackson"; // first character char ch1 = input.charAt(0); // third character char ch3 = input.charAt(2); // tenth character char ch10 = input.charAt(9); System.out.println("Character at 0 index is "+ch1); System.out.println("Character at 2 index is "+ch3); System.out.println("Character at 9 index is "+ch10); >>