【发布时间】: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