📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Iterating over the characters of a string is a fundamental operation in many programming tasks, such as parsing, validation, or text transformation. Java provides multiple ways to accomplish this, each suitable for different scenarios and requirements. This blog post will explore several methods to iterate over the characters of a string in Java, including for-loops, streams, and the forEach method.
2. Program Steps
1. Use a traditional for-loop to iterate over characters by index.
2. Use an enhanced for-loop with the toCharArray method.
3. Use the chars method introduced in Java 8 to create an IntStream, then iterate over it.
4. Use the codePoints method for full Unicode support, including surrogate pairs.
3. Code Program
public class StringIterationExample {
public static void main(String[] args) {
String str = "Hello, Java!";
// Step 1: Using a traditional for-loop
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
System.out.println("Character at " + i + ": " + ch);
}
// Step 2: Using an enhanced for-loop with toCharArray
for (char ch : str.toCharArray()) {
System.out.println("Character: " + ch);
}
// Step 3: Using chars method (Java 8+)
str.chars().forEach(ch -> System.out.println("Character: " + (char) ch));
// Step 4: Using codePoints method for full Unicode support (Java 8+)
str.codePoints().forEach(cp -> System.out.println("Code point: " + cp + ", Character: " + (char) cp));
}
}
Output:
Character at 0: H Character at 1: e Character at 2: l Character at 3: l Character at 4: o Character at 5: , Character at 6: Character at 7: J Character at 8: a Character at 9: v Character at 10: a Character at 11: ! Character: H Character: e Character: l Character: l Character: o Character: , Character: Character: J Character: a Character: v Character: a Character: ! Character: H Character: e Character: l Character: l Character: o Character: , Character: Character: J Character: a Character: v Character: a Character: ! Code point: 72, Character: H Code point: 101, Character: e Code point: 108, Character: l Code point: 108, Character: l Code point: 111, Character: o Code point: 44, Character: , Code point: 32, Character: Code point: 74, Character: J Code point: 97, Character: a Code point: 118, Character: v Code point: 97, Character: a Code point: 33, Character: !
Explanation:
1. Traditional for-loop: Iterates over the string by index, using charAt to access each character. This method is suitable for cases where the index is needed.
2. Enhanced for-loop with toCharArray: This method converts the string into a character array and iterates over it. It is clean but involves creating a new array.
3. Using chars method: Introduced in Java 8, chars returns an IntStream of character codes, allowing for functional-style operations. It's convenient for simple character operations but lacks full Unicode support.
4. Using codePoints method: Also introduced in Java 8, codePoints returns an IntStream of Unicode code points, fully supporting all Unicode characters, including supplementary characters (represented by surrogate pairs in UTF-16). This method is recommended for applications requiring complete Unicode support.
Comments
Post a Comment
Leave Comment