From d3d60f392b77c69677e02dfc8407df7e5b1aea72 Mon Sep 17 00:00:00 2001 From: Neetika23 <42495275+Neetika23@users.noreply.github.com> Date: Thu, 26 Oct 2023 23:41:32 +0530 Subject: [PATCH] Update StringIterator.java --- .../stringIterator/StringIterator.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/core-java-modules/core-java-strings/src/main/java/com/baeldung/stringIterator/StringIterator.java b/core-java-modules/core-java-strings/src/main/java/com/baeldung/stringIterator/StringIterator.java index 2ffabc48d4..cbd69a151a 100644 --- a/core-java-modules/core-java-strings/src/main/java/com/baeldung/stringIterator/StringIterator.java +++ b/core-java-modules/core-java-strings/src/main/java/com/baeldung/stringIterator/StringIterator.java @@ -1,14 +1,53 @@ 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){ str.chars().forEach(name -> { 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(); + } + } }