```val string = "8% of 25 is the same as <img c...
# announcements
m
Copy code
val string = "8% of 25 is the same as <img class=\"limit\">There are no heroes in a punk rock band <img class=\"limit\">"

        val arraySearch = arrayOf("<img class=\"limit\">", "<img class=\"limit\">")

        val arrayReplacement = arrayOf("<img class=\"another-limit\" data-id=\"1\">", "<img class=\"another-limit\" data-id=\"2\">")

        arraySearch.forEachIndexed { index, it ->
            string.replace(it, arrayReplacement[index])
        }
println(string)
the string doesn't get replaced. still showing
8% of 25 is the same as <img class="limit">There are no heroes in a punk rock band <img class="limit">
where it should have been
8% of 25 is the same as <img class="another-limit" data-id="1">There are no heroes in a punk rock band <img class="another-limit" data-id="2">
y
string.replace
returns the replaced string, but it doesn't modify the original string, instead you want to have
var string
and then do
string = string.replace(...)
m
your answer is close. shows me:
8% of 25 is the same as <img src="/someimage.png" class="another-limit" data-id="2">There are no heroes in a punk rock band <img src="/someimage.png" class="another-limit" data-id="2">
which is unexpected
notice the data-id values don't match
y
that's because you're replacing the same value twice I think. Use
string.replaceFirst
instead
Copy code
var string = "8% of 25 is the same as <img class=\"limit\">There are no heroes in a punk rock band <img class=\"limit\">"

val arraySearch = arrayOf("<img class=\"limit\">", "<img class=\"limit\">")

val arrayReplacement = arrayOf("<img class=\"another-limit\" data-id=\"1\">", "<img class=\"another-limit\" data-id=\"2\">")

arraySearch.forEachIndexed { index, it ->
    string = string.replaceFirst(it, arrayReplacement[index])
}

println(string)
m
tried with
listOf()
Copy code
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 images = "<img src=\"/someimage.png\" class=\"another-limit\" data-id=\"1\">,<img src=\"/someimage.png\" class=\"another-limit\" data-id=\"2\">"

        val arraySearch = listOf("<img src=\"/someimage.png\" class=\"limit\">", "<img src=\"/someimage.png\" class=\"limit\">")

        val arrayReplacement = images.split(",")

        // var replacedString = ""
        arraySearch.forEachIndexed { index, it ->
            string = string.replace(it, arrayReplacement[index])
        }

        println(string)
        return string
shows
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 is 1 all over the place. why is that?
y
Again try using
string.replaceFirst
. Check the code snippet that I just sent btw because I tried it out and it works
m
yea it works. but what if I want to assign it to an empty string?
Copy code
var replacedString = ""
        arraySearch.forEachIndexed { index, it ->
            replacedString = string.replaceFirst(it, arrayReplacement[index])
        }

        println(replacedString)
this shows me:
8% of 25 is the same as <img src="/someimage.png" class="another-limit" data-id="2">There are no heroes in a punk rock band <img src="/someimage.png" class="limit">
y
Then do this:
Copy code
var replacedString = string
        arraySearch.forEachIndexed { index, it ->
            replacedString = replacedString.replaceFirst(it, arrayReplacement[index])
        }
        println(replacedString)
❤️ 1
💯 1
m
Thank you so much.
y
No problem at all
n
`val replaced = arraySearch.zip(arrayReplacement).fold(string) { acc, (s, r) -> acc.replaceFirst(s, r) }`is another way to accomplish what you want
💯 1