zt
10/04/2022, 12:55 AMval values = match.groupValues.drop(1) // ["kotlin slack"]
var newResponse = filter.response!! // "hi $1"
values.forEachIndexed { index, value ->
newResponse = filter.response!!.replace("\$$index", value)
}
// newResponse = "hi kotlin slack"
Is there some cleaner way of doing this? I wanna replace $0
, $1
, $2
, etc, with its corresponding valueephemient
10/04/2022, 1:45 AMrepsonse.replace("\$([0-9])".toRegex()) { match ->
values[match.group(1).toInt()]
}
Michael de Kaste
10/04/2022, 9:26 AM"hi $3, $1 and $2"
.replace("""\$(\d+)""".toRegex()) { "%${it.groupValues[1]}\$s" }
.format("tom", "alice", "bob")
// hi bob, tom and alice
ephemient
10/04/2022, 9:43 AM%
in the original stringMichael de Kaste
10/04/2022, 9:47 AM