用匹配的字符串替换不同html但相同文本的字符串 [英] Replacing a string with the matched string with different html but same text

查看:80
本文介绍了用匹配的字符串替换不同html但相同文本的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String1:

<img alt="" src="http://abcghgds.com/justin-bieber-ferns-650-430.jpg" width="650" height="430" /> Have you seen <a href="http://www.abcdefg.com/between_two_ferns" target="_blank">Between Two Ferns</a>?

结果1:

Have you seen <a   style = "display:inline" href="http://www.abcdefg.com/between_two_ferns" target="_blank">Between Two Ferns</a>?

我正在尝试检查文本是否为string1以result1中的文本结尾。如果是的话那么我想用String替换String1中的文本(与result1中的文本相同)

I'm trying to check if the text is string1 ends with the text in result1. If it does then I want to replace the text(which is same as text in result1) in String1 with ""

所以基本上在上面的情况下,预期的输出将是:(因为string1中的文本以result1中的文本结尾)我想要此输出

So basically in the above case the expected output would be:(As text in string1 ends with the text in result1) I want this output

String 1 = <img alt="" src="http://abcghgds.com/justin-bieber-ferns-650-430.jpg" width="650" height="430" /> 

虽然文本相同,但它包含的关联html是不同的。所以我无法真正取代它。

Though the text is same, the associated html it is wrapped into is different. So I can't really replace it .

这是我试过的东西

String ans1= Jsoup.parse(string1).text();
String ans2 = Jsoup.parse(result1).text();

    if(ans1.endsWith(ans2))
    {   
        string1=string1.replace(result1, ""); ---> // This does not work as I have to replace the text as well as the html from the original string to get the desired op. How do I do it? 
    }


推荐答案

你忘了写所有数据你做的是读取和操作数据

you forgot to write data all you did was read and manipulate data

 String ans1= Jsoup.parse(string1).text();
 String ans2 = Jsoup.parse(result1).text();

if(ans1.endsWith(ans2))
{   
    string1=string1.replace(result1, "");
}
Jsoup.parse(ans1).text(string1);

这篇关于用匹配的字符串替换不同html但相同文本的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆