Atul Gupta
04/22/2024, 7:39 AMAnnotatedString
with the replacement string in such a way that replace method returns AnnotatedString
string itself?Atul Gupta
04/22/2024, 7:41 AM%PLACEHOLDER%
string which I want to replace at single place with some other actual value of placeholderMark
04/23/2024, 12:28 PMfun CharSequence.replaceAnnotated(
regex: Regex,
transform: (MatchResult) -> AnnotatedString?,
): AnnotatedString {
val input = this
var offset = 0
return buildAnnotatedString {
regex.findAll(input).forEach { mr ->
val replacement = transform(mr) ?: return@forEach
val first = mr.range.first
if (first > offset) {
append(input.subSequence(offset, first))
}
append(replacement)
offset = mr.range.last + 1
}
if (offset < input.length) {
append(input.subSequence(offset, input.length))
}
}
}