怎么把HTML格式的转成字符串

如题所述

一个String显示在网页上,不会安置原来的格式显示,比如说,回车符在网页上就显示成了一个空格,下面这个方法可以。把HTML格式的转成字符串
public static String toHTMLString(String in) {
StringBuffer out = new StringBuffer();
for (int i = 0; in != 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();
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-15
<html>
<body >
<a id="test">
<input type="text" value="helloworld!"/>
</a>
<script type="text/javascript">
alert(document.getElementById("test").innerHTML);
</script>
</body>
</html>

本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网