首页 > 解决方案 > Kotlin 字符串替换

问题描述

如何替换kotlin中的字符串?我在这里有 html 字符串的示例。

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>"

是否可以选择在 < iframe> 标签中仅添加 instagram 链接,在链接末尾添加“嵌入”字符串,因此输出将如下所示:

"<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>"

标签: kotlin

解决方案


我会用特定库(如DOMParser-kotlin)解析 html 字符串,然后循环查找 iframe 并修改 src 属性


推荐阅读