mending3
02/24/2021, 1:41 PMval 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">
Youssef Shoaib [MOD]
02/24/2021, 1:52 PMstring.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(...)
mending3
02/24/2021, 2:02 PM8% 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 unexpectedmending3
02/24/2021, 2:03 PMYoussef Shoaib [MOD]
02/24/2021, 2:06 PMstring.replaceFirst
insteadYoussef Shoaib [MOD]
02/24/2021, 2:07 PMYoussef Shoaib [MOD]
02/24/2021, 2:07 PMvar 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)
mending3
02/24/2021, 2:27 PMlistOf()
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?Youssef Shoaib [MOD]
02/24/2021, 2:32 PMstring.replaceFirst
. Check the code snippet that I just sent btw because I tried it out and it worksmending3
02/24/2021, 2:36 PMvar 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">
Youssef Shoaib [MOD]
02/24/2021, 2:41 PMvar replacedString = string
arraySearch.forEachIndexed { index, it ->
replacedString = replacedString.replaceFirst(it, arrayReplacement[index])
}
println(replacedString)
mending3
02/24/2021, 2:43 PMYoussef Shoaib [MOD]
02/24/2021, 2:46 PMnkiesel
02/24/2021, 7:53 PM