I am receiving some large String content that contains several numbers demarcating sections.
I'm trying to modify the content in such a way that if a number is present, a line break is inserted.
So that something like: "1.This is one line. 2. This is another. 3. Here is one more."
gets modified to something like this:
"1.This is one line.
2. This is another.
3. Here is one more."
I tried solving it this way:
fun modifyText(inputStr: String):String {
val marker= listOf('1', '2', '3', '4', '5', '6', '7')
var text = ""
marker.forEach {
if (inputStr.contains(it)) {
text = inputStr.replace(it.toString(), "\n $it" )
}
}
return text
}
This only modified the section with the last number and not everything. Thought of using recursion this way:
tailrec fun modifyText(inputStr: String):String {
val marker= listOf('1', '2', '3', '4', '5', '6', '7')
var text = ""
marker.forEach {
if (inputStr.contains(it)) {
text = inputStr.replace(it.toString(), "\n $it" )
}
}
return modifyText(text)
}
but ended up "bricking" the app.
Please any solutions to how this can be achieved will be appreciated.