Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.66 KB | None | 0 0
  1. func lchop*(s, sub: string): string =
  2.   ## Remove ``sub`` from the beginning of ``s``.
  3.   runnableExamples:
  4.     let s = "ABcde"
  5.     doAssert s.lchop("AB") == "cde"
  6.     doAssert s.lchop("XXX") == "ABcde"
  7.  
  8.   if s.startsWith(sub):
  9.     s[sub.len .. s.high]
  10.   else:
  11.     s
  12.  
  13. func rchop*(s, sub: string): string =
  14.   ## Remove ``sub`` from the end of ``s``.
  15.   runnableExamples:
  16.     let fname = "stuff.nim"
  17.     doAssert fname.rchop(".nim") == "stuff"
  18.     doAssert fname.rchop(".jpg") == "stuff.nim"
  19.     let longer = "nim is a great language"
  20.     doAssert longer.rchop("uage") == "nim is a great lang"
  21.  
  22.   if s.endsWith(sub):
  23.     s[0 ..< ^sub.len]
  24.   else:
  25.     s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement