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.
This is mostly API bikeshedding in that you still have to import std/strutils in Nim anyway even if it is in the stdlib while in Python you get that as part of string objects. I.e., if you want to use them a lot it's easy (some might say "trivial") to wrap:
import std/strutils
proc lstrip(s: string): string = s.strip(trailing=false)
proc rstrip(s: string): string = s.strip(leading=false)
echo " a b c ".rstrip.repr
echo " a b c ".lstrip.repr and there are non-stdlib Nimble packages to ease making Nim look more like Python, such as:
https://github.com/nimpylib/nimpylib/blob/master/examples/example2.nim
As Araq says you could also definitely "up your game" in terms of parsing strategies (although most "compiler authors" have a stronger connection to exactly such upped games).
The nature of defaulted keywords arguments suggests that Python could grow defaulted leading= and trailing= arguments to its .strip() in a way that is fully backward compatible within Python. People might find the more verbose forms more readable than "[lr]strip()". Python is kind of a big ocean-liner and I'd bet it's hard to get this kind of change into it and Nim is a small community more amenable to "changing to fit in", but if you ignore that aspect, it isn't obvious which move would be "better" in terms of cross-compatibility. Of course, both stdlibs doing both is the most cross-compatible of all, but such will always be quite limited in spite of various superficial syntax similarities - intentionally. Since Nim is not a Python-compiler, you should not have super-high expectations.
Anyway, I'm not really advocating anything as much as trying to provide a little more detail with a little more diplomacy.
Thanks for the answers. Sometimes I don't want to parse the string, just remove something from the beginning or the end. For instance, getHomeDir() adds a trailing '/'. Usually I don't need that, so this is what I write:
let HOME = getHomeDir().rstrip("/") # /home/user
But I can live without these two convenience functions :)