how would you and extension function that removes ...
# announcements
s
how would you and extension function that removes a bunch of characters from a String?
Copy code
Fun String.removeChars(allTheCharsToRemove: String): String
there is the String.replace(regex, replacement) that you could call with
""
as second parameter. So I’d have to create a Regex from my
allTheCharsToRemove
(how? if that’d be your approach) or is there a better way?
f
String.strip
maybe? In any event, if you want to remove literals iterate and do not create a RegExp, way too much overhead.
r
If Kotlin StdLib doesn’t offer anything: https://pl.kotl.in/wmhdqCH18 Depending on the datatype of
allTheCharsToRemove
Copy code
fun String.removeAll(toRemove:String) = this.removeAll(toRemove.toCharArray())

fun String.removeAll(toRemove:CharArray) : String = filterNot(toRemove::contains)
👍 2