Scala : Idiomatic way of removing set of suffixes (substrings) from a String -
i'm looking idiomatic way of stripping set of suffixes (substrings) string in scala.
val inputstring = "abc@xyz.com" val suffixes = list("\"", "@xyz.com")
basically, suffixes list loaded externally , hence not static. therefore cannot use approach of chaining stripsuffix methods
(p.s. - i'll remove starting " using inputstring.stripprefix("\"")
you used foldleft
along .stripsuffix()
function iterate on dynamic list of suffixes , strip them string one-by-one.
val inputstring = "abc@xyz.com" val suffixes = list("\"", "@xyz.com") val strippedstring = suffixes.foldleft(inputstring) { (string, suffix ) => string.stripsuffix(suffix) }
outputs: abc
with approach order of suffixes impact output.
Comments
Post a Comment