方式1:
一個String顯示在網頁上,不會安置原來的格式顯示,比如說,斷行符號符在網頁上就顯示成了一個空格,下面這個方法可以將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(); }
方式 2:
HtmlDocument String2Html(string htmlString) { WebBrowser getInfoWebBrowser = new WebBrowser(); getInfoWebBrowser.ScriptErrorsSuppressed = true; getInfoWebBrowser.DocumentText = htmlString; HtmlDocument html = getInfoWebBrowser.Document; Application.DoEvents(); getInfoWebBrowser.Dispose(); return html; }