⬅ Previous Topic
Java String Methods - Syntax and DescriptionNext Topic ⮕
Java StringcodePointAt()
methodcharAt()
method⬅ Previous Topic
Java String Methods - Syntax and DescriptionNext Topic ⮕
Java StringcodePointAt()
methodStrings are fundamental in Java and often need to be manipulated. The charAt()
method provides a way to access individual characters within a string by their index.
public char charAt(int index)
Parameter | Description |
---|---|
index |
The index of the character to retrieve. This is a zero-based index, meaning the first character is at index 0. |
The charAt()
method returns a char
representing the character at the specified index in the string.
This example demonstrates how to get the first character of a string using an index of 0.
public class CharAtExample {
public static void main(String[] args) {
String str = "Hello";
char firstChar = str.charAt(0);
System.out.println("The first character is: " + firstChar);
}
}
The first character is: H
Here, we declare a string str
as "Hello". Then, we call the charAt(0)
method to retrieve the character at index 0 which is 'H'. Finally, it prints the retrieved character.
This example shows how to access a specific character in the string by providing its index. Let's retrieve the character at index 2.
public class CharAtExample {
public static void main(String[] args) {
String str = "Java";
char middleChar = str.charAt(2);
System.out.println("The character at index 2 is: " + middleChar);
}
}
The character at index 2 is: v
In this example, we take the string 'Java' and access the element at index 2 which corresponds to the character 'v'. The method returns this value.
This demonstrates what happens when you try to access an invalid (out-of-bounds) index. Attempting to retrieve a character beyond the string's length will result in a StringIndexOutOfBoundsException.
public class CharAtExample {
public static void main(String[] args) {
String str = "Hello";
try {
char outOfBoundsChar = str.charAt(10); // Index 10 is out of bounds.
System.out.println("Character at index 10: " + outOfBoundsChar);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: Index Out of Bounds - " + e.getMessage());
}
}
}
Error: Index Out of Bounds - String index out of range [0-4]
This example uses a try-catch
block to handle the potential IndexOutOfBoundsException
. Since we're trying to access an element at index 10 in "Hello" (which is only 5 characters long), Java throws this exception, which is then caught and displayed.
Here's how you can use `charAt()` to iterate through all the characters in a string.
public class CharAtExample {
public static void main(String[] args) {
String str = "World";
for (int i = 0; i < str.length(); i++) {
char currentChar = str.charAt(i);
System.out.println("Character at index " + i + ": " + currentChar);
}
}
}
Character at index 0: W
Character at index 1: o
Character at index 2: r
Character at index 3: l
Character at index 4: d
This loop iterates from index 0 to the last character (length - 1). In each iteration, it retrieves a character using charAt(i)
and prints its value. This is a common technique when you need to process each individual character in a string.
⬅ Previous Topic
Java String Methods - Syntax and DescriptionNext Topic ⮕
Java StringcodePointAt()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.