About 10,900,000 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. Method 2: Using String.toCharArray () method. In this approach, we convert string to a character array using String.toCharArray () method.

  2. What is the easiest/best/most correct way to iterate through the ...

    Jan 6, 2017 · If you need to iterate through the code points of a String (see this answer) a shorter / more readable way is to use the CharSequence#codePoints method added in Java 8: for(int c : string.codePoints().toArray()){ ... } or using the stream directly instead of a for loop: string.codePoints().forEach(c -> ...);

  3. 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.

  4. 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.

  5. java - 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));

  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. 3 Ways for Traversal of Strings - GeeksforGeeks

    Dec 17, 2023 · Use a for loop to iterate over each character in the string. In each iteration, the loop variable holds the current character. You can print or process the character within the loop. For Example: Initialize a variable with the string you want to traverse. Initialize an index variable (e.g., index) to 0.

  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. Iterating Over String Characters in Java - Java Guides

    Java provides several ways to iterate over the characters of a string, each with its own use cases and benefits. In this blog post, we'll explore various methods to iterate the String characters in Java. Method 1: Using charAt() in a Loop

  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.

  11. Some results have been removed
Refresh