I have this code:
val string = "<p></p>this<p></p>"
val arrs = listOf("<p data-id=\"1\"></p>", "<p data-id=\"2\"></p>")
var replacedString = ""
arrs.forEach {
val pTag = "<p></p>"
replacedString = string.replace(pTag, it) // <-- I know this is the culprit, but I got stuck. don't know what to do anymore
}
println(replacedString)
It should show <p data-id="1"></p>this<p data-id="2"></p> , but it shows
<p data-id="2"></p>this<p data-id="2"></p> instead
notice the data-id value is incrementing in accordance to the list
The main problem is that the string variable and the listOf will be dynamic.
for example:
val string = "<p></p>this<p></p>that<p></p>"
val arrs = listOf("<p data-id=\"1\"></p>", "<p data-id=\"2\"></p>", "<p data-id=\"3\"></p>")
or
val string = "<p></p>this<p></p><p></p>that"
val arrs = listOf("<p data-id=\"1\"></p>", "<p data-id=\"2\"></p>", "<p data-id=\"3\"></p>")
so on and so forth