val string = "8% of 25 is the same as <img src=\"/someimage.png\" class=\"limit\">There are no heroes in a punk rock band <img src=\"/someimage.png\" class=\"limit\">"
val arraySearchInTheStringVariableAbove = arrayOf(
"<img src=\"/someimage.png\" class=\"limit\">",
"<img src=\"/someimage.png\" class=\"limit\">"
)
val arrayReplacementInTheStringVariableAbove = arrayOf(
"<img src=\"/someimage.png\" class=\"another-limit\" data-id=\"1\">",
"<img src=\"/someimage.png\" class=\"another-limit\" data-id=\"2\">"
)
arraySearchInTheStringVariableAbove.forEachIndexed { index, it ->
string.replace(it, arrayReplacementInTheStringVariableAbove[index])
}
println(string)
the string doesn't get replaced. still shows:
8% of 25 is the same as <img src="/someimage.png" class="limit">There are no heroes in a punk rock band <img src="/someimage.png" class="limit">
where it should have been
8% of 25 is the same as <img src="/someimage.png" class="another-limit" data-id="1">There are no heroes in a punk rock band <img src="/someimage.png" class="another-limit" data-id="2">
notice the <img> tag gets replaced sequentially
Stringis immutable, soreplacedoesn't modify the object its called on, but returns a new modified one instead. You ignore that return value, effectively making the whole method call pointless.replaceinto the array.