Java Program to Iterate Over Characters in String Last Updated : 06 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Given string str of length N, the task is to traverse the string and print all the characters of the given string using java. Illustration: Input : str = “GeeksforGeeks” Output : G e e k s f o r G e e k sInput : str = "GfG" Output : G f G Methods: Using for loops(Naive approach)Using iterators (Optimal approach) Method 1: Using for loops The simplest or rather we can say naive approach to solve this problem is to iterate using a for loop by using the variable 'i' till the length of the string and then print the value of each character that is present in the string. Example Java // Java Program to Iterate Over Characters in String // Class 1 // Main class // To iterate over characters class GFG { // Method 1 // To traverse the string and // print the characters of the string static void getChar(String str) { // Traverse the string using for loop for (int i = 0; i < str.length(); i++) { // Printing the current character System.out.print(str.charAt(i)); // Printing a space after each letter System.out.print(" "); } } // Method 2 // Main driver method public static void main(String[] args) { // Creating a String variable to store the string String str = "GeeksforGeeks"; // Calling the getChar method getChar(str); } } OutputG e e k s f o r G e e k s Time complexity is O(N) and space complexity is O(1) Method 2: Using iterators The string can be traversed using an iterator. We would be importing CharacterIterator and StringCharacterIterator classes from java.text package Example: Java // Java Program to Iterate Over Characters in String // Importing input output classes import java.io.*; // Importing CharacterIterator and StringCharacterIterator // classes from java.text package import java.text.CharacterIterator; import java.text.StringCharacterIterator; // Main class // To iterate over characters class GFG { // Method 1 // To traverse the string and // print the characters of the string static void getChar(String str) { // Creating a CharacterIterator variable CharacterIterator itr = new StringCharacterIterator(str); // Iterating using while loop while (itr.current() != CharacterIterator.DONE) { // Print the current character System.out.print(itr.current()); // Print a space after each letter System.out.print(" "); // Getting the next input from the user // using the next() method itr.next(); } } // Method 2 // Main driver method public static void main(String[] args) { // Creating a String variable to store the string String str = "GfG"; // Calling the getChar method getChar(str); } } OutputG f G Time Complexity: O(N) and space complexity is of order O(1) Comment More infoAdvertise with us Next Article Java Program to Iterate Over Characters in String kunalmali Follow Improve Article Tags : Java Java Programs Java-String-Programs Practice Tags : Java Similar Reads Iterate Over the Characters of a String in Java Given string str of length N, the task is to traverse the string and print all the characters of the given string. Illustration: Input : âGeeksforGeeksâ Output : G e e k s f o r G e e k sInput. : âCoderâ Output : C o d e r Methods: Using Naive ApproachUsing String.toCharArray() methodUsing Character 9 min read Java Program to Add Characters to a String We will be discussing out how to add character to a string at particular position in a string in java. It can be interpreted as follows as depicted in the illustration what we are trying to do which is as follows: Illustration: Input: Input custom string = HelloOutput: --> String to be added 'Gee 4 min read Java Program to Replace Multiple Characters in a String In this program, we will be discussing various methods for replacing multiple characters in String. This can be done using the methods listed below: Using String.replace() methodUsing replaceAll() method Using replaceFirst() method Method 1: Using String.replace() method This method returns a new st 3 min read Convert String to Stream of Chars in Java The StringReader class from the java.io package in Java can be used to convert a String to a character stream. When you need to read characters from a string as though it were an input stream, the StringReader class can be helpful in creating a character stream from a string. In this article, we wil 2 min read Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided 2 min read Java Program to Count the Occurrences of Each Character In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. This process involves identifying the frequency of each character in the input string and displaying it in a readable format.Example: Input/output to count the occurrences o 5 min read Java Program to Count Number of Digits in a String The string is a sequence of characters. In java, objects of String are immutable. Immutable means that once an object is created, it's content can't change. Complete traversal in the string is required to find the total number of digits in a string. Examples: Input : string = "GeeksforGeeks password 2 min read Java Program to Count Number of Vowels in a String In java, the string is a sequence of characters and char is a single digit used to store variables. The char uses 2 bytes in java. In java, BufferedReader and InputStreamReader are used to read the input given by the user from the keyboard. Then readLine() is used for reading a line. The java.io pac 4 min read Java Program to Check Whether the String Consists of Special Characters In Java, special characters refer to symbols other than letters and digits, such as @, #, !, etc. To check whether the String consists of special characters, there are multiple ways, including using the Character class, regular expressions, or simple string checks.Example:In this example, we will us 4 min read Convert a String to Character Array in Java Converting a string to a character array is a common operation in Java. We convert string to char array in Java mostly for string manipulation, iteration, or other processing of characters. In this article, we will learn various ways to convert a string into a character array in Java with examples.E 2 min read Like