Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

slicing an ASCII text

Nim 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.

(1) the diff
s = "Batman"

print(s[0:3])  # Bat; first three characters
let s = "Batman"

echo s[0..3]    # Batm; closed interval

echo s[0..<3]   # Bat; position 3 is NOT included

Nim uses a range expression (ex.: echo s[0..3]), which defines a closed interval. In Python, the interval is open on the right side. Thus, if you want a Python-like slicing, write s[0..<3], which makes an interval that is closed on the left side and open on the right side, just like in Python.

(2) examples
s = "Batman"

print(s[0])  # B

print(s[0:3])  # Bat
print(s[:3])  # Bat

print(s[1:4])  # atm

print(s[3:6])  # man
print(s[3:200])  # man; right side is truncated
print(s[3:])  # man

print(s[-1])  # n

print(s[:-1])  # Batma

print(s[1:-1])  # atma
let s = "Batman"

echo s[0]  # B

echo s[0..<3]   # Bat
echo s[0..<3]   # Bat; both positions must be indicated

echo s[1..<4]   # atm

echo s[3..<6]   # man
#echo s[3..200]   # ERROR: out of range
echo s[3 .. s.high]   # man

echo s[^1]  # n

echo s[0 ..< s.high]  # Batma

echo s[1 ..< s.high]  # atma

echo s.low  # 0; index of the first element
echo s.high  # 5; index of the last element

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 Unicode

Let'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 s[0..<2] doesn't mean "take the first two characters". No! It means "take the first 2 bytes". And that gives 'L' and half of the character 'á'. That's why we get that funny '�' sign.

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.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 06, 14:28