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:
public char charAt(int index)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:
Example 1: Finding first, mid and last index of the given string.
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);
}
}
Output:
Character at 0 index is m
Character at 2 index is c
Character at 9 index is a
3. Example 2: IndexOutOfBoundsException when using charAt() method
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 15
at java.lang.String.charAt(Unknown Source)
at examples.java.w3schools.string.StringCharAtExample.main(StringCharAtExample.java:7)
4. Example 3: Printing String char by char using charAt() method
package examples.java.w3schools.string;
public class StringCharAtExample {
public static void main(String[] args) {
String input = "iterate";
for(int i=0; i< input.length();i++){
System.out.println(input.charAt(i));
}
}
}
Output:
it
e
r
a
t
e
5. Example 4: Printing even number index characters
package examples.java.w3schools.string;
public class StringCharAtExample {
public static void main(String[] args) {
String input = "123456789";
for(int i=0;i< input.length();i=i+2){
System.out.println("Index "+i+" : "+input.charAt(i));
}
}
}
Output:
Index 0 : 1Index 2 : 3
Index 4 : 5
Index 6 : 7
Index 8 : 9
6. Example 5: Printing an odd number of index characters
package examples.java.w3schools.string;
public class StringCharAtExample {
public static void main(String[] args) {
String input = "123456789";
for(int i=1;i< input.length();i=i+2){
System.out.println("Index "+i+" : "+input.charAt(i));
}
}
}
Output:
Index 1 : 2Index 3 : 4
Index 5 : 6
Index 7 : 8
7. Example 6: Counting number of occurrences of a character
public class StringCharAtExample {
public static void main(String[] args) {
int count = 0;
String input = "abbbccbbbc";
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'b') {
count++;
}
}
System.out.println("No. of occurrences of b character is : " + count);
}
}
Output:
No. of occurrences of b character is: 6Please post your questions in the comments section.
8. Conclusion
In this tutorial, We've seen when to use charAt() method in java and example programs to get even or odd indexed values.
Java String API
More String Methods
No comments:
Post a Comment
Please do not add any spam links in the comments section.