About 12,800 results
Open links in new tab
  1. Iterate Over the Characters of a String in Java - GeeksforGeeks

    Feb 28, 2022 · The simplest approach to solve this problem is to iterate a loop over the range [0, N – 1], where N denotes the length of the string, using the variable i and print the value of str[i]. Example Java

  2. How do I apply the for-each loop to every character in a String?

    Mar 16, 2010 · If you use Java 8 with Eclipse Collections, you can use the CharAdapter class forEach method with a lambda or method reference to iterate over all of the characters in a String. Strings.asChars("xyz").forEach(c -> System.out.print(c));

  3. Java Program to Iterate Over Characters in String

    Jun 6, 2021 · 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. Time complexity is O (N) and space complexity is O (1) Method 2: Using iterators.

  4. java - How to iterate through a String - Stack Overflow

    Sep 27, 2010 · Using Guava (r07) you can do this: for(char c : Lists.charactersOf(someString)) { ... } This has the convenience of using foreach while not copying the string to a new array. Lists.charactersOf returns a view of the string as a List.

  5. java - Inserting characters in a string via for loop - Stack Overflow

    Jan 27, 2017 · You can follow following code snippet using loop (as you mentioned) to add character to string. String s = "HelloWorld"; String y =""; Character c = '*'; int length = s.length(); for(int i = 1; i<= length; i++) { if(i == length) y += s.substring(i-1, i); else y += s.substring(i-1, i) + c; } System.out.println(y);

  6. How to Iterate Over the String Characters in Java | Baeldung

    May 11, 2024 · In Java, there are several ways to iterate over the characters of a string, each with its own time and space complexity. The best method to use depends on the specific requirements of your program. We can use a simple for loop to iterate over the characters of a string.

  7. Java For Loop - W3Schools

    When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: Syntax for ( statement 1 ; statement 2 ; statement 3 ) { // code block to be executed }

  8. Java Program to Iterate through each characters of the string.

    Example 2: Loop through each character of a string using for-each loop class Main { public static void main(String[] args) { // create a string String name = "Programiz"; System.out.println("Characters in string \"" + name + "\":"); // loop through each element using for-each loop for(char c : name.toCharArray()) { // access each character ...

  9. Java For Loop - GeeksforGeeks

    Apr 17, 2025 · Java for loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The for loop in Java provides an efficient way to iterate over a range of values, execute code multiple times, or traverse arrays and collections. Now let's go through a simple Java for l

  10. Iterate over characters of a String in Java - Techie Delight

    Jan 14, 2022 · This post will discuss various methods to iterate over characters in a string in Java. 1. Naive solution. A naive solution is to use a simple for-loop to process each character of the string. This approach proves to be very effective for strings of smaller length. 2. Using String.toCharArray() method.

Refresh