我有一个要求。我有一个字符串,其值为例如:
<p>We are pleased <a href="http://www.anc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html">to present the new product type</a>. This new product type is the best thing since sliced bread. We are pleased to present the new product type. This new product <a href="mailto:abc@gmail.com">type is the best</a> thing since sliced bread.</p>
上述文本将存储为单个字符串值。检查条件后,我需要将某些参数附加到 href。让我知道如何仅提取 href 并附加参数并显示字符串而不损坏(仅供参考:字符串是通过 RTE - 富文本编辑器输入的值)
尝试了这种方法,但没有成功。
String tmpStr = "href=\"http://www.abc.com\">design";
StringBuffer tmpStrBuff = new StringBuffer();
String[] tmpStrSpt = tmpStr.split(">");
if (tmpStrSpt[0].contains("abc.com")) {
String[] tmpStrSpt1 = tmpStrSpt[0].split("\"");
tmpStrBuff.append(tmpStrSpt1[0]);
if (tmpStrSpt1[1].contains("?")) {
tmpStrBuff.append("\"" + tmpStrSpt1[1] + "&s_cid=abcd_xyz\">");
} else {
tmpStrBuff.append("\"" + tmpStrSpt1[1] + "?s_cid=abcd_xyz\">");
}
tmpStrBuff.append(tmpStrSpt[1]);
tmpStrBuff.append("</a>");
System.out.println(" <p>tmpStr1:::: " + tmpStrBuff.toString() + "</p>");
}
使用的另一种方法是:
String[] tmpTxtArr = text.split("\\s+");
StringBuffer tmpStrBuff = new StringBuffer();
for (String tmpTxt : tmpTxtArr) {
descTxt += (tmpTxt.contains("abc.com") && !tmpTxt.contains("?")) ? tmpTxt
.replace("\">", "?s_cid=" + trackingCode + "\">" + " ")
: tmpTxt + " ";
}