0

How to replace string in kotlin? I have example here with html string.

var test = "<html><body><p> just some test text</p> And i wanna use this text for texting and i ll show you this image https://www.instagram.com/p/B8I9_KiF45g/?utm_source=ig_web_copy_link also this is the instagram photo with just normal link id body https://www.instagram.com/p/B8I3r66pVpp/ <h> random title text </h></body></html>"

Is there an option to add only instagram links in < iframe> tags, add 'embed' string at the end of the link, so output would be like this:

"<html><body><p> just some test text</p> And i wanna use this text for texting and i ll show you this image <iframe src="https://www.instagram.com/p/B8I9_KiF45g/embed"> </iframe> also this is the instagram photo with just normal link id body <iframe src="https://www.instagram.com/p/B8I3r66pVpp/embed" </iframe> <h> random title text </h></body></html>"
1
  • 1
    If you want to use special characters in strings, escape them like: "<iframe src=\"https:....\"" Commented Feb 4, 2020 at 10:58

2 Answers 2

1

This is not perfect (as regex matching HTML has some issues) but it might work for your specific use case and should at least get you started. It uses a combination of RegEx and replace (bundled in an extension function) to find links and embed them:

fun String.embedded(): String {
    // Match everything starting with https://www.instagram.com/ until the first question mark, whitespace or new tag.
    // The second group is used to get rid of any query parameters.
    val linkRegex = "(https://www.instagram.com/[^\\? <]*)(\\?[^ <]*)?".toRegex()
    // $1 is the first matching group, i.e. the link itself.
    val output = this.replace(linkRegex, """<iframe src="$1embed"> </iframe>""")
    return output
}

Short sample for your provided data:

fun main() {
    val input = "<html><body><p> just some test text</p> And i wanna use this text for texting and i ll show you this image https://www.instagram.com/p/B8I9_KiF45g/?utm_source=ig_web_copy_link also this is the instagram photo with just normal link id body https://www.instagram.com/p/B8I3r66pVpp/ <h> random title text </h></body></html>"
    val expected = """<html><body><p> just some test text</p> And i wanna use this text for texting and i ll show you this image <iframe src="https://www.instagram.com/p/B8I9_KiF45g/embed"> </iframe> also this is the instagram photo with just normal link id body <iframe src="https://www.instagram.com/p/B8I3r66pVpp/embed"> </iframe> <h> random title text </h></body></html>"""

    val output = input.embedded()
    println(output)
    println(output == expected)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I think this is what I need.
1

I would parser the html string with specific library like DOMParser-kotlin and then loop for the iframes and modify the src attributes

2 Comments

But I do not have iframes, need to add them also.
Oh yes, I see. Maybe you can split the string by spaces and search for the substrings that starts by "https" and concatenate ifram tag at the beginning and at the end. And with this substring tagged replace the indexOf('?') to length by embed. Take care with the special characters as the question comment said

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.