|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
slicing an ASCII textNim has string slicing, and it's very similar to Python's approach. As long as you want to slice an ASCII text, no problem :) If you want to slice a text that has some fancy Unicode characters, then we'll need a slightly different approach. Let's keep it simple and see how slicing works on an ASCII string.
Nim uses a range expression (ex.:
With slicing, you can also modify some part of a mutable string: var s = "batman" s[0..<3] = "BAT" echo s # BATman Of course, if your string is immutable, you can still have the same result: import std/strutils # strip, split, join let s = "batman" t = s[0..<3].toUpper & s[3 .. s.high] echo t # BATman Problem with UnicodeLet's see the problem with Unicode characters: let s = "László" # my name, BTW :) echo s[0..<2] # we expect the first two characters: Lá Output: L� The root of the problem is the following: strings are stored in UTF-8 format. The character 'á' is stored in 2 bytes. And In Python it works as expected, we'd get "Lá". Python, as usual, tries to help us and does a lot of extra work behind the scenes. But we'll see it in detail later. |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |
||||||||