How to iterate through strings in java?

The easiest or naive way to iterate or traverse through a string is to use a simple for-loop to process each character of the string over the range [0, N – 1], where N denotes the length of the string, using a variable let's say 'i' and print the value of str[i]

public static void main(String[] args)
    {
        String str = "Chercher Tech";
        // using simple for-loop
        for (int i = 0; i < str.length(); i++) {
            System.out.print(str.charAt(i));
        }
    }

Output-

Chercher Tech

Using toCharArray() method -

Another way to iterate over a string is to convert the string to a character array using the toCharArray() method and then iterate over the character array using for loop or for each loop as shown below-

public static void main(String[] args) {
  String str = "Chercher Tech";

  // convert string to 'char[]' array
  char[] charArr = str.toCharArray();

  // iterate over 'char[]' array using enhanced for-loop
  for (char ch: charArr) {
    System.out.print(ch);
  }
}

Output-

Chercher Tech

Using Iterator -

We can also iterate through a string by using the StringCharacterIterator class.

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class Main {
  // Iterate over the characters of a string
  public static void main(String[] args) {

    String str = "Chercher Tech";
    //below syntax constructs an iterator with an initial index of 0.
    CharacterIterator it = new StringCharacterIterator(str);

    //The current() method returns the character at the current position
    //or DONE if the current position is off the end of the text.
    while (it.current() != CharacterIterator.DONE) {
      System.out.print(it.current() + " ");
      it.next();
    }
  }
}

Output-

C h e r c h e r   T e c h

You can also iterate from a specified initial index as shown below-

public static void main(String[] args) {
  String str = "Chercher Tech";
  
  //Below syntax constructs an iterator with the specified initial index 3
  CharacterIterator it = new StringCharacterIterator(str, 3);

  //current() returns the character at the current position
  //or DONE if the current position is off the end of the text.
  while (it.current() != CharacterIterator.DONE) {
    System.out.print(it.current() + " ");
    it.next();
  }
}

Output-

r c h e r   T e c h

To iterate over the given range of any string, with the index set at the specified position, you can use the constructor index- StringCharacterIterator(str, int begin, int end, int pos)

as shown below-
 public static void main(String[] args) {
   String str = "Chercher Tech";

   CharacterIterator it = new StringCharacterIterator(str, 3, 7, 5);
   //current() returns the character at the current position
   //or DONE if the current position is off the end of the text.
   while (it.current() != CharacterIterator.DONE) {
     System.out.print(it.current() + " ");
     it.next();
   }
 }

Output-

h e

Using split() method -

By using the split() method we can split the string at space or any other special character like $, :, /, etc and then store the split strings in a string array and then iterate through the array using for loop as shown below-

 public static void main(String[] args) {
   String str = "Chercher Tech";

   String[] arr = str.split("");

   for (String s: arr) {
     System.out.print(s);
   }
 }
Output -
Chercher Tech
These are the ways through which we can iterate over a string.
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions