Skip to main content

APIzation: Replication Package

[Q#3799130][A#3799176] How to iterate through a String

https://stackoverflow.com/q/3799130

How can I iterate through a string in Java? I'm trying to use a foreach style for loop

Answer

https://stackoverflow.com/a/3799176

If you want to use enhanced loop, you can convert the string to charArray

APIzation

for (char ch : exampleString.toCharArray()){
        System.out.println(ch);
    }
package com.stackoverflow.api;

public class Human3799176 {

  public static char[] convertStringToCharArray(String exampleString) {
    char[] result = exampleString.toCharArray();
    for (char ch : result) {
      System.out.println(ch);
    }
    return result;
  }
}

package com.stackoverflow.api;

/**
 * How to iterate through a String
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/3799176">https://stackoverflow.com/a/3799176</a>
 */
public class APIzator3799176 {

  public static void iterate(String exampleString) throws Exception {
    for (char ch : exampleString.toCharArray()) {
      System.out.println(ch);
    }
  }
}