Logo

Programming-Idioms

  • Fortran
  • Java

Idiom #286 Iterate over characters of a string

Print a line "Char i is c" for each character c of the string s, where i is the character index of c in s (not the byte index).

Make sure that multi-byte characters are properly handled, and count for a single character.

for(int i=0; i<s.length(); i++) {
  char c = s.charAt(i);
  System.out.println("Char " + i + " is " + c); 
}

Internally, each char is 16-bit
import static java.lang.System.out;
import static java.text.Normalizer.Form.NFKC;
import static java.text.Normalizer.normalize;
s = normalize(s, NFKC);
int i, n = s.length();
for (i = 0; i < n; ++i)
    out.printf("Char %s is %c%n", i, s.charAt(i));
import static java.util.stream.IntStream.range;
range(0, s.length())
    .forEach(i -> {
        out.printf("Char %s is %s%n", i, s.charAt(i));
    });
import java.util.concurrent.atomic.AtomicInteger;
AtomicInteger i = new AtomicInteger(0);
s.chars().forEach(v -> {
  char c = (char)v;
  System.out.println("Char " + i.getAndIncrement() + " is " + c); 
});

chars() returns an IntStream
use iso_fortran_env
  integer, parameter :: ucs4  = selected_char_kind ('ISO_10646')
  character(kind=ucs4,  len=30) :: s
open (output_unit, encoding='UTF-8')

do i=1,len(s)
  print *,"Char ", i, " is ", s(i:i)
end do

s has to be declared to be of the right kind for Unicode. The open statement makes sure that standard output is UTF-8.
(dorun (map-indexed (fn [i c] (println "Char" i "is" c)) s))

In Clojure a string is a list of characters

New implementation...