Java - Iterate Over the Characters of a String

By Preeti Jain Last updated : April 8, 2024

There are several approaches to iterate over the characters of a string, here we will discuss the three simplest approaches:

  1. Using For Loop
  2. Using toCharArray() of String
  3. Using Java Streams

Iterate Over the Characters of a String using For Loop

In this approach, we will iterate the for loop from (0 to n-1) where n represents the length of the String, using the counter variable (i) and display the value of s(i).

Example

// Program to Iterate Over the Characters 
// of a String in Java
// Using Regular Approach

import java.io.*;
import java.util.*;

class TraverseString {
  public static void main(String[] args) {
    // Static Input
    String s = "IncludeHelpIsATechBlog";

    // Iterate the loop from [i to s.length()] and print
    // the value of [s.charAt(i)]
    for (int i = 0; i < (s.length()); i++) {
      // Display current character
      System.out.print(s.charAt(i) + " ");
    }

  }
}

The output of the above example is:

I n c l u d e H e l p I s A T e c h B l o g 

Iterate Over the Characters of a String using toCharArray() Method

In this approach, first, we will convert String(s) into char array by using the toCharArray() method of the String class like this [String.toCharArray()] and then iterate the converted char array using for-each loop.

Example

// Program to Iterate Over the Characters 
// of a String in Java
// Using toCharArray() of String approach

import java.io.*;
import java.util.*;

public class TraverseString {
  // This method traverse String by first converting 
  // String to Char array 
  // using s.toCharArray() and then after iterate 
  // char array using for-each loop
  static void processString(String s) {
    char[] chars = s.toCharArray();
    // Traverse the character array using for-each loop
    for (char c: chars) {
      // Display current character
      System.out.print(c + " ");
    }
  }
  public static void main(String[] args) {
    // Static Input 
    String s = "IncludeHelpIsATechBlog";
    // Invoking processString
    processString(s);
  }
}

The output of the above example is:

I n c l u d e H e l p I s A T e c h B l o g 

Iterate Over the Characters of a String using Java Streams

In this approach, we will use Java Streams 8 it simply converts String(s) into the stream of chars using str.chars() and then traverses each element of the char stream using forEach() and we need to typecast (char) ch because chars() return integer stream.

Example

// Program to Iterate Over the Characters 
// of a String in Java
// Using Java Streams

public class TraverseString {
  // This method traverse String by first 
  // converting String to Stream 
  // of chars using str.chars() and then 
  // after iterate char stream using forEach loop
  static void processString(String s) {
    s.chars().forEach(ch -> {
      System.out.print((char) ch + " ");
    });
  }

  public static void main(String[] args) {
    // Static Input
    String s = "IncludehelpIsATechBlog";
    // Invoking processString
    processString(s);
  }
}

The output of the above example is:

I n c l u d e h e l p I s A T e c h B l o g 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.