public static String toHTMLString(String in) {
StringBuffer out = new StringBuffer()
for (int i = 0in != null &&i <in.length()i++) {
char c = in.charAt(i)
if (c == '\'')
out.append("'")
else if (c == '\"')
out.append(""")
else if (c == '<')
out.append("<")
else if (c == '>')
out.append(">")
else if (c == '&')
out.append("&")
else if (c == ' ')
out.append(" ")
else if (c == '\n')
out.append("<br/>")
else
out.append(c)
}
return out.toString()
}
<DIV id='div1'><table><tr><td>aa</td></tr></table>
</DIV>
<script language="javascript">
while (div1.children.length >0)
{
var children = div1.children
for (var i=children.length-1i>=0i--)
{
var child = children[i]
child.outerHTML = "[" + child.tagName + "]" + child.innerHTML + "[/" + child.tagName + "]"
}
}
alert (div1.innerHTML)
</script>
一个String显示在网页上,不会安置原来的格式显示,比如说,回车符在网页上就显示成了一个空格,下面这个方法可以将String改为HTML可以辨认的格式。
public static String toHTMLString(String in) {
StringBuffer out = new StringBuffer()
for (int i = 0in != null &&i <in.length()i++) {
char c = in.charAt(i)
if (c == '\'')
out.append("'")
else if (c == '\"')
out.append(""")
else if (c == '<')
out.append("<")
else if (c == '>')
out.append(">")
else if (c == '&')
out.append("&")
else if (c == ' ')
out.append(" ")
else if (c == '\n')
out.append("<br/>")
else
out.append(c)
}
return out.toString()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)