This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

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.

s.split('').asMap().forEach((i, c) => print('Char $i is $c'));
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
i := 0
for _, c := range s {
	fmt.Printf("Char %d is %c\n", i, c)
forM_ (zip [0 ..] s) (\(i, c) -> putStrLn $ "Char " ++ show i ++ " is " ++ [c])
for (const [i, c] of [...s].entries()) {
	console.log(`Char ${i} is ${c}`);
s.forEachIndexed { i, c ->
  println("Char $i is $c")
for i := 1 to utf8length(s) do
  writeln(format('Char %d is %s',[i, utf8copy(s,i,1)]));
for i, c in enumerate(s):
    print(f'Char {i} is {c}')
s.each_char.with_index{|c, i| puts "Char #{i} is #{c}" }
for (i, c) in s.chars().enumerate() {
    println!("Char {} is {}", i, c);

New implementation...