📘 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
Method 1: Using charAt() in a Loop
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
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:
Method 3: Using Java 8 Streams
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
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.
Comments
Post a Comment
Leave Comment