【问题标题】:Replace a substring with a StringBuffer substring用 StringBuffer 子字符串替换子字符串
【发布时间】:2014-01-07 17:27:58
【问题描述】:

我有一个巨大的字符串,它是由 JSOUP 获得的完整 html 字符串。我使用 String Bufer replace API(replace(int startIndex,int endIndex, "to be changed string) 对 html 的子字符串进行了更改。字符串缓冲区已完美填充。但是当我尝试用新的字符串缓冲区替换 html 的子字符串时,它不起作用。

这里是代码sn-p。

html = html.replace(divStyle1.trim(), heightwidthM.toString().trim());

最初的大html是

<!DOCTYPE html>
<html xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="SAF" id="global-header-light">
<head>

</head>
<body>


**<div style="background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height: 2059px; width: 1001px; text-align: center; margin: 0 auto;">**                      

<div style="height:2058px; padding-left:0px; padding-top:36px;">


<iframe style="height:90px; width:728px;"/>



</div>
</div>

</body>
</html>

divStyle1 字符串是

background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height: 2059px; width: 1001px; text-align: center; margin: 0 auto;

字符串缓冲区有值

background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height:720px; width:900px; text-align: center; margin: 0 auto;

在 divStyle 是最后一个 HTML 的子字符串(在字符串中)并且 heightwidthM 是必须替换它的 Stringbuffer 值时不起作用。它不会抛出任何错误,但它也不会改变它。

谢谢 斯瓦拉杰

【问题讨论】:

  • 值是什么?如果html 很大,只需发布​​字符串的相关部分。你确定有完全匹配吗?
  • 考虑到您正在使用 jsoup,而 jsoup 让您在处理 html 文档时更加轻松,为什么不在您的 html 中创建一个 org.jsoup.nodes.Document,找到您需要的标签并在适当的位置进行修改?
  • @rgettman 进行了必要的编辑

标签: java string jsoup stringbuffer


【解决方案1】:

使用 JSoup 很容易做到这一点

String html = "<!DOCTYPE html>\n<html xmlns:og=\"http://opengraphprotocol.org/schema/\" xmlns:fb=\"http://www.facebook.com/2008/fbml\" xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\" class=\"SAF\" id=\"global-header-light\">\n<head>\n\n</head>\n<body>\n\n\n**<div style=\"background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height: 2059px; width: 1001px; text-align: center; margin: 0 auto;\">**                      \n\n<div style=\"height:2058px; padding-left:0px; padding-top:36px;\">\n\n\n<iframe style=\"height:90px; width:728px;\"/>\n\n\n\n</div>\n</div>\n\n</body>\n</html>";
String newStyle = "background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height:720px; width:900px; text-align: center; margin: 0 auto;";

Document document = Jsoup.parse(html);
document.body().child(0).attr("style", newStyle);
System.out.println(document.html());

【讨论】:

    【解决方案2】:

    回到我的建议,如果你不介意尝试,你可以做这样的事情:

    Document newDocument = Jsoup.parse(<your html string>, StringUtils.EMPTY, Parser.htmlParser());
    Elements yourStyles = newDocument.select("div[style]"); // this will select all div with attributes style
    yourStyles.get(0).attr("style", <your new value>); // this will get your first div and replace attribute style to your new value
    System.out.println(newDocument.outerHtml());
    

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2015-04-02
      • 2017-03-23
      • 2013-03-10
      • 2017-04-24
      • 2016-12-24
      • 2012-08-09
      • 2014-10-12
      • 2017-12-06
      相关资源
      最近更新 更多