Scala has dropWhilewhich solves half the problem. It also has dropRightan analogue dropfor the right end of the collection. Unfortunately, he doesn’t dropWhileRight, so you need to be creative.
If you don’t really care about efficiency, you can just drop the characters from the left end, on the contrary, repeat and reverse:
scala> "untidy stringnu".dropWhile(s).reverse.dropWhile(s).reverse
res0: String = tidy string
If you are sure that this will be a bottleneck in your program (hint: maybe not), you need some imperative solution.
source
share