Java Program to Iterate each Characters of String.
What is String in Java ?
In Java, the “String” data type is a class that represents a sequence of characters. It is used to store and manipulate text-based data. Unlike primitive data types, such as “int” or “float”, which hold a single value, a String object can hold multiple characters.
More on String in Java .
In Java, strings are immutable, which means that once a string object is created, it cannot be changed. Instead, any operation that modifies a string actually creates a new string object. This is why it’s important to be mindful of the performance implications when performing a lot of string operations.
Steps to Convert int to String :
- Declare the string you want to iterate over.
- Get the length of the string using the length() method.
- Set up a loop to iterate over each character in the string, from index 0 to length – 1.
- Inside the loop, use the charAt() method to get the character at the current index.
- Do whatever operation you want on the current character.
- Repeat steps 4-5 for each character in the string.
- Exit the loop when all characters have been processed.
Here’s a pseudo-code of the algorithm :
// Step 1: Declare the string you want to iterate over. string myString = "example"; // Step 2: Get the length of the string using the length() method. int length = myString.length(); // Step 3: Set up a loop to iterate over each character in the string. for (int i = 0; i < length; i++) { // Step 4: Use the charAt() method to get the character at the current index. char currentChar = myString.charAt(i); // Step 5: Do whatever operation you want on the current character. // For example, print it to the console. System.out.println(currentChar); } // Step 6: Repeat steps 4-5 for each character in the string. // Step 7: Exit the loop when all characters have been processed.
Example 1 :
public class Main { public static void main(String[] args) { String myString = "Prepinsta"; int length = myString.length(); for (int i = 0; i < length; i++) { char currentChar = myString.charAt(i); System.out.print(currentChar+" "); } } }
Output :
P r e p i n s t a
Example 2 :
public class Main { public static void main(String[] args) { String myString = "PrepinstaPrime"; char[] charArray = myString.toCharArray(); int i = 0; while (i < charArray.length) { char currentChar = charArray[i]; System.out.print(currentChar+" "); i++; } } }
Output :
P r e p i n s t a P r i m e
We then set up a while loop to iterate over each character in the array. Inside the loop, we use the current value of i to access the current character in the array, and then print it to the console using System.out.print().
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment