Add docs
This commit is contained in:
parent
99b29b3e97
commit
d2c992c03d
1 changed files with 27 additions and 0 deletions
|
|
@ -1396,6 +1396,20 @@ proc format*(formatstr: string, a: varargs[string, `$`]): string {.noSideEffect,
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
proc removeSuffix*(s: var string, chars: set[char] = Newlines) {.rtl.} =
|
proc removeSuffix*(s: var string, chars: set[char] = Newlines) {.rtl.} =
|
||||||
|
## Removes the first matching character from the string (in-place) given a
|
||||||
|
## set of characters. If the set of characters is only equal to `Newlines`
|
||||||
|
## then it will remove both the newline and return feed.
|
||||||
|
## .. code-block:: nim
|
||||||
|
## var
|
||||||
|
## userInput = "Hello World!\r\n"
|
||||||
|
## otherInput = "Hello!?!"
|
||||||
|
## userInput.removeSuffix
|
||||||
|
## userInput == "Hello World!"
|
||||||
|
## userInput.removeSuffix({'!', '?'})
|
||||||
|
## userInput == "Hello World"
|
||||||
|
## otherInput.removeSuffix({'!', '?'})
|
||||||
|
## otherInput == "Hello!?"
|
||||||
|
|
||||||
var last = len(s) - 1
|
var last = len(s) - 1
|
||||||
|
|
||||||
if chars == Newlines:
|
if chars == Newlines:
|
||||||
|
|
@ -1412,9 +1426,22 @@ proc removeSuffix*(s: var string, chars: set[char] = Newlines) {.rtl.} =
|
||||||
s.setLen(last + 1)
|
s.setLen(last + 1)
|
||||||
|
|
||||||
proc removeSuffix*(s: var string, c: char) {.rtl.} =
|
proc removeSuffix*(s: var string, c: char) {.rtl.} =
|
||||||
|
## Removes a single character (in-place) from a string.
|
||||||
|
## .. code-block:: nim
|
||||||
|
## var
|
||||||
|
## table = "users"
|
||||||
|
## table.removeSuffix('s')
|
||||||
|
## table == "user"
|
||||||
removeSuffix(s, chars = {c})
|
removeSuffix(s, chars = {c})
|
||||||
|
|
||||||
proc removeSuffix*(s: var string, suffix: string) {.rtl.} =
|
proc removeSuffix*(s: var string, suffix: string) {.rtl.} =
|
||||||
|
## Remove the first matching suffix (in-place) from a string.
|
||||||
|
## .. code-block:: nim
|
||||||
|
## var
|
||||||
|
## answers = "yeses"
|
||||||
|
## answers.removeSuffix("es")
|
||||||
|
## answers == "yes"
|
||||||
|
|
||||||
var newLen = s.len
|
var newLen = s.len
|
||||||
|
|
||||||
if s.endsWith(suffix):
|
if s.endsWith(suffix):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue