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.
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.
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));
range(0, s.length())
.forEach(i -> {
out.printf("Char %s is %s%n", i, s.charAt(i));
});
binmode STDOUT, ":utf8";
while ($s =~ /(\X)/g) {
say 'Char ' . pos($s) . ' is ' . $1;
}
Import pragma 'use utf8' is necessary if string $s is set from a string inside your souce code. If read from stdin, you'll need binmode STDIN, ':utf8' as well. The while loop does a pattern match looking for any unicode character (\X) and repeats that multiple times (the g modifier). The pos() function returns the position of the match. Parens are used around the \X to capture the character and return it in $1. The . operator is for string concatenation.
for i, c in enumerate(s):
print(f'Char {i} is {c}')
Python strings are sequences of Unicode codepoints (not bytes).