Scala String trimming by a set of characters -


given set of (trailing) characters, instance

val s = "un".toset 

how trim string s, namely,

"untidy stringnu".trimby(s) res: string = tidy string 

scala has dropwhile solves half of problem. has dropright that's analog drop right end of collection. unfortunately doesn't have dropwhileright, though, have creative.

if don't particularly care efficiency, can drop characters off left end, reverse, repeat, , reverse again:

scala> "untidy stringnu".dropwhile(s).reverse.dropwhile(s).reverse res0: string = tidy string 

if you're sure that's going bottleneck in program (hint: it's not), you'll want kind of imperative solution.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -