Is there something in the standard library that al...
# codereview
m
Is there something in the standard library that already does this?
Copy code
class StringReplacer(private val replacementMap: Map<String, String>) {
    private val regex = replacementMap.keys
        .joinToString(separator = "|") { "\\Q$it\\E" }
        .toRegex()

    fun replace(input: CharSequence) = regex.replace(input) { mr ->
        replacementMap[mr.value]!!
    }
    
    companion object {
        operator fun invoke(vararg pairs: Pair<String, String>) = StringReplacer(mapOf(*pairs))
    }
}
­čÜź 1