1
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

2
  • 4
    String is immutable, so replace doesn'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. Commented Feb 24, 2021 at 13:59
  • To make this work you'd need to put the new string created by replace into the array. Commented Feb 24, 2021 at 14:00

2 Answers 2

4

Java and Kotlin strings are immutable and not editable in place. replace() returns a new string that you're throwing away.

One way to fix that is change the val string to a var string and assign the returned string back to it.

Sign up to request clarification or add additional context in comments.

Comments

3

String is immutable, every time you transform it, it creates new String in StringPool, you can assign it back to same string as var

var 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 = string.replace(it, arrayReplacementInTheStringVariableAbove[index])
}

println(string)

3 Comments

tried with listOf. I notice weird behavior when the data-id value is always 1. this the code: pastebin.com/jDm7U6ic .
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="1"> . notice the data-id value
use replaceFirst instead, because replace replaces all occurences

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.