|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching • Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
Iterate over the characters of a Unicode stringSee std/unicode Problemimport std/strformat # &"Hello {name}!" let name = "László" for idx, c in name: echo &"{idx}: {c}" Output: 0: L 1: � 2: � 3: s 4: z 5: l 6: � 7: � Reason: strings are stored in UTF-8 format, and 'á' and 'ó' occupy 2 bytes. When you iterate over the characters, you actually iterate over each byte. In Nim, a char is an unsigned byte. SolutionA Unicode character in UTF-8 can occupy 1, 2, 3 or 4 bytes. A import std/strformat # &"Hello {name}!" import std/unicode # Rune let name = "László" for idx, c in name.toRunes(): echo &"{idx}: {c}" Output: 0: L 1: á 2: s 3: z 4: l 5: ó proc toRunes(s: string): seq[Rune] 𝥶It returns a sequence of Runes. iterator runes(s: openArray[char]): Rune 𝥶This is just an iterator. If you want to process every character of a huge text, then it can be cheaper. |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |