Use split proc
proc split(s: string; seps: set[char] = Whitespace; maxsplit: int = - 1): seq[string] {..}
to use
from strutils import split
for token in "My string when splitted".split(maxsplit = 1):
echo token
split on Whitespace and splitWhitespace are not equivalent:
from strutils import split, splitWhitespace
let s = " a couple of \t words "
echo s.split.len # prints 9
echo s.splitWhitespace.len # prints 4
In case of leading whitespace split(maxsplit = 1)[0] is empty string, while I expect splitWhitespace(maxsplit = 1) to be the first non-whitespace token in the string. Sure, one can strip the leading whitespace before using split(), etc.
But as I see, all the functionality for splitWhitespace(maxsplit = <something>) is already there, it is just not exposed via public splitWhitespace interface.
The question is "why?" Is it buggy or what?
The optional maxsplit parameter was added to strutils.splitWhitespace.
For details see: https://github.com/nim-lang/Nim/issues/6503
@olwi
I'd say it looks like a bug. I always use split though.
@Udiknedormin
What exactly looks like a bug?
How split can't behave like splitWhitespace. I guess it should and just be more general.
Well, there is a similar library in Fortran. If I recall, there is a function a little similar to split (it's also an iterator). It separates the concept of a separator characters and unmatched characters. So it would be something like that:
echo " a couple of \t words ".split(sep = Whitespace)
# @[, , a, couple, of, , , words, ]
echo " a couple of \t words ".split(ignore = Whitespace)
# @[a, couple, of, words]
2) all other forms of split work like they do now. To get the current default behaviour of split one should use s.split(Whitespace) In other words:
echo " a couple of \t words ".split()
# @[a, couple, of, words]
echo " a couple of \t words ".split(Whitespace)
# @[, , a, couple, of, , , words, ]
This is easy to implement, but as far as I understand that would constitute a breaking change...