🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Method 1: Using charAt() in a Loop
The most straightforward method to iterate over characters in a string is using a for loop along with the charAt() method.
Example:
public class StringIteration {
public static void main(String[] args) {
String str = "Hello, World!";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
System.out.println("Character at position " + i + " is: " + ch);
}
}
}
Output:
Character at position 0 is: H Character at position 1 is: e Character at position 2 is: l Character at position 3 is: l Character at position 4 is: o Character at position 5 is: , Character at position 6 is: Character at position 7 is: W Character at position 8 is: o Character at position 9 is: r Character at position 10 is: lCharacter at position 11 is: d Character at position 12 is: !
Explanation:
- We use a for loop to go through each index of the string.
- charAt(i) returns the character at the specified index.
Method 2: Using toCharArray() and a For-Each Loop
Another method is to convert the string to a character array using toCharArray() and then use a for-each loop.
Example:
public class StringIteration {
public static void main(String[] args) {
String str = "Hello, World!";
for (char ch : str.toCharArray()) {
System.out.println("Character: " + ch);
}
}
}
Output:
Character: H Character: e Character: l Character: l Character: o Character: , Character: Character: W Character: o Character: r Character: l Character: d Character: !
Explanation:
toCharArray() converts the string into a character array.
The for-each loop iterates over each character in the array.
Method 3: Using Java 8 Streams
For a more functional approach, especially in Java 8 and later, you can use streams to iterate over characters.
Example:
import java.util.stream.IntStream;
public class StringIteration {
public static void main(String[] args) {
String str = "Hello, World!";
IntStream charsStream = str.chars();
charsStream.forEach(ch -> System.out.println("Character: " + (char) ch));
}
}
Output:
Character: HCharacter: e Character: l Character: l Character: o Character: , Character: Character: W Character: o Character: r Character: l Character: d Character: !
Explanation:
- str.chars() creates an IntStream representing the character values of the string.
- forEach is used to iterate over each character in the stream.
Method 4: Using codePoints() for Unicode Strings
If you are dealing with Unicode strings that may have surrogate pairs, use codePoints().
Example:
public class StringIteration {
public static void main(String[] args) {
String str = "Hello, 👋 World!";
str.codePoints().forEach(cp -> System.out.println("Codepoint: " + cp + ", Character: " + new String(Character.toChars(cp))));
}
}
Output:
Codepoint: 72, Character: H Codepoint: 101, Character: e Codepoint: 108, Character: l Codepoint: 108, Character: l Codepoint: 111, Character: o Codepoint: 44, Character: , Codepoint: 32, Character: Codepoint: 128075, Character: 👋 Codepoint: 32, Character: Codepoint: 87, Character: W Codepoint: 111, Character: o Codepoint: 114, Character: r Codepoint: 108, Character: l Codepoint: 100, Character: d Codepoint: 33, Character: !
Explanation:
- codePoints() returns a stream of Unicode code points.
- The forEach method is used to iterate over each code point.
Conclusion
Iterating the string characters in Java can be done in multiple ways, each suitable for different scenarios. Traditional loops work well for simple tasks, while streams offer a more modern, functional approach, particularly useful for complex operations or when working with Unicode.
Understanding these techniques is essential for Java developers, as string manipulation is a core part of many programming tasks.
Stay tuned for more Java tips and techniques. Happy coding!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment