Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Iterate Through Each Character of a String in Java



In this article, we will understand how to iterate through each character of the string using Java. The string is a datatype that contains one or more characters and is enclosed in double quotes (" "). Char is a datatype that contains an alphabet an integer or a special character.

Problem Statement

Write a program to Iterate through each character of the string. Below is a demonstration of the same ?

Input

The string is defined as: Java Program

Output

The characters in the string are:
J, a, v, a, , P, r, o, g, r, a, m,

Different approaches

The following are the different approaches to Iterate through each character of the string ?

Using for-loop to iterate through each character of the string

Following are the steps to iterate through each character of the string using for loop?

  • Define a string variable input_string with the value "Java Program".
  • Print the defined string using a print statement.
  • Loop through each character of the string using a for-loop.
  • Use the charAt() method to get each character of the string by index.
  • Print each character followed by a comma.
  • End the loop when all characters are printed.

Example

Below is an example to iterate through each character of the string using the for loop ?

public class Characters {
   public static void main(String[] args) {
      String input_string = "Java Program";
      System.out.println("The string is defined as: " +input_string);
      System.out.println("The characters in the string are: ");
      for(int i = 0; i<input_string.length(); i++) {
         char temp = input_string.charAt(i);
         System.out.print(temp + ", ");
      }
   }
}

Output

The string is defined as: Java Program
The characters in the string are:
J, a, v, a, , P, r, o, g, r, a, m,

Using for each loop to iterate through each character of the string

The following are the steps to iterate through each character of the string using for each loop ?

  • Define a string variable input_string with the value "Java Program".
  • Print the defined string using a print statement.
  • Convert the string into a character array using the toCharArray() method.
  • Use a for-each loop to iterate through each character in the array.
  • Print each character followed by a comma.

Example

Below is an example to iterate through each character of the string using for each loop ?

public class Main {
   public static void main(String[] args) {
      String input_string = "Java Program";
      System.out.println("The string is defined as: " +input_string);
      System.out.println("The characters in the string are: ");
      for(char temp : input_string.toCharArray()) {
         System.out.print(temp + ", ");
      }
   }
}

Output

The string is defined as: Java Program
The characters in the string are:
J, a, v, a, , P, r, o, g, r, a, m,
Updated on: 2024-09-29T02:48:31+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements