繁体   English   中英

用StringBuffer子字符串替换子字符串

[英]Replace a substring with a StringBuffer substring

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

这是代码片段。

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;

并且字符串缓冲区具有值

背景图片:url( http://aka-cdn-ns.adtech.de/rm/ads/23274/HPW​​omenLOFT_1381687318.jpg) ;背景重复:不重复; -webkit-background-size:1001px 2059px; 高度:720像素; 宽度:900px; 文本对齐:居中; 保证金:0自动;

在divStyle是最后一个HTML(在String中)的子字符串并且heightwidthM是必须替换为其的Stringbuffer值的情况下不起作用,它不会引发任何错误,但也不会对其进行更改。

感谢Swaraj

使用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());

回到我的建议,如果您不介意尝试,则可以执行以下操作:

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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM