for each string java code example

Example 1: iterate over string java

String str = "hello";
for (char c : str.toCharArray())
  //process c

Example 2: java foreach char

for (char ch: "xyz".toCharArray()) {
  System.out.println(ch); 
}

Example 3: for each loop string java

String s = "xyz"
for (char ch: s.toCharArray()) {
  System.out.println(ch);
}

Example 4: for each c++

template<class InputIterator, class Function>
  Function for_each(InputIterator first, InputIterator last, Function fn)
{
  while (first!=last) {
    fn (*first);
    ++first;
  }
  return fn;      // or, since C++11: return move(fn);
}

Tags:

Cpp Example