I want to iterate a string using the for loop which I can do
let s = ".something"
for c in s
#no problem
but I want to start after the first character which I can also do
let s = ".something"
for c in s[1..<len(s)]
#no problem
what I want to know is if there might be a better way to do this? I'm thinking maybe like this but it might just be a matter of preference or would one be faster than the other?
let s = ".something"
for c in s[1..^1]
#better?
When performance is really critical you should test it yourself, otherwise all solutions are ok.
Second and third example should be identical. The question is of both of them do copy the initial string s. I don't know, but a copy is fast. You may also consider using pairs iterator, which gives you index of character, so you can put a if condition in the loop to skip processing for first char. Or you may use something like "for i in 1 .. s.high" and access chars with s[i].
https://nim-lang.org/docs/system.html#pairs.i%2CopenArray%5BT%5D
You can use this with (almost?) no overhead:
iterator span(s: string; first: int, last: BackwardsIndex): char =
for i in first..s.len - last.int: yield s[i]
let s = ".something"
for c in s.span(1, ^1):
echo c