Nim has a strip() method, but lstrip() and rstrip() are missing. They can be implemented easily (see below), but I think they would have a place in the stdlib, in std/strutils. When I work with strings, I need them very often.
import std/strutils
func lstrip*(s: string, chars: set[char] = Whitespace): string =
s.strip(leading=true, trailing=false, chars=chars)
func lstrip*(s: string, chars: string): string =
var bs: set[char] = {}
for c in chars:
bs = bs + {c}
s.strip(leading=true, trailing=false, chars=bs)
func rstrip*(s: string, chars: set[char] = Whitespace): string =
s.strip(leading=false, trailing=true, chars=chars)
func rstrip*(s: string, chars: string): string =
var bs: set[char] = {}
for c in chars:
bs = bs + {c}
s.strip(leading=false, trailing=true, chars=bs)
Usage examples:
import std/strutils
echo("'" & " aa bb ".strip() & "'") # 'aa bb'
echo("'" & " aa bb ".lstrip() & "'") # 'aa bb '
echo("'" & " aa bb ".rstrip() & "'") # ' aa bb'
echo("'" & "line\n".rstrip("\n") & "'") # 'line' When I work with strings, I need them very often.
That means you're doing it wrong. ;-) Use strscans to extract what you need or a use a real parser or a parser generator or parsecsv, etc.