Update StringIterator.java

This commit is contained in:
Neetika23 2023-10-26 23:41:32 +05:30 committed by GitHub
parent 450b6d8025
commit d3d60f392b
1 changed files with 40 additions and 1 deletions

View File

@ -1,9 +1,33 @@
package com.baeldung.stringIterator;
import java.text.*;
import java.util.*;
public class StringIterator {
public static void main(String[] args) {
String str = "Hello, Baeldung!";
javaforLoop(str);
System.out.println();
java8forEach(str);
System.out.println();
javaCharArray(str);
System.out.println();
javaRegexExp(str);
System.out.println();
javaCharacterIterator(str);
}
public static void javaCharArray(String str){
for (char c : str.toCharArray()) {
System.out.print(c);
}
}
public static void javaforLoop(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
System.out.print(c);
}
}
public static void java8forEach(String str){
@ -11,4 +35,19 @@ public class StringIterator {
System.out.print((char) name);
});
}
public static void javaRegexExp(String str){
String[] characters = str.split("");
for (String c : characters) {
System.out.print(c);
}
}
public static void javaCharacterIterator(String str){
CharacterIterator it = new StringCharacterIterator(str);
while (it.current() != CharacterIterator.DONE) {
System.out.print(it.current());
it.next();
}
}
}