C#中HTML字符轉(zhuǎn)換函數(shù)分享
更新時(shí)間:2012年07月27日 09:07:39 作者:
在ASP.Net中經(jīng)常會(huì)從網(wǎng)面中取數(shù)據(jù)或更新網(wǎng)頁的顯示。因?yàn)镠TML中有些特殊字符如<, >, &等,顯示實(shí)際值不一致,造成保存到數(shù)據(jù)庫再取出來時(shí)會(huì)不一樣
因此需要以下函數(shù)做轉(zhuǎn)換:
///<summary>
///替換html中的特殊字符
///</summary>
///<paramname="theString">需要進(jìn)行替換的文本。</param>
///<returns>替換完的文本。</returns>
public static string HtmlEncode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace("\"",""");
theString = theString.Replace("\'", "'");
theString=theString.Replace("\n","<br/>");
return theString;
}
///<summary>
///恢復(fù)html中的特殊字符
///</summary>
///<paramname="theString">需要恢復(fù)的文本。</param>
///<returns>恢復(fù)好的文本。</returns>
public static string HtmlDiscode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace(""","\"");
theString = theString.Replace("'", "\'");
theString=theString.Replace("<br/>","\n");
return theString;
}
復(fù)制代碼 代碼如下:
///<summary>
///替換html中的特殊字符
///</summary>
///<paramname="theString">需要進(jìn)行替換的文本。</param>
///<returns>替換完的文本。</returns>
public static string HtmlEncode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace("\"",""");
theString = theString.Replace("\'", "'");
theString=theString.Replace("\n","<br/>");
return theString;
}
///<summary>
///恢復(fù)html中的特殊字符
///</summary>
///<paramname="theString">需要恢復(fù)的文本。</param>
///<returns>恢復(fù)好的文本。</returns>
public static string HtmlDiscode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace(""","\"");
theString = theString.Replace("'", "\'");
theString=theString.Replace("<br/>","\n");
return theString;
}
相關(guān)文章
一次.net?core異步線程設(shè)置超時(shí)時(shí)間的實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于.net?core異步線程設(shè)置超時(shí)時(shí)間的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
vs.net 2010 擴(kuò)展插件小結(jié) 提高編程效率
本文價(jià)紹了幾款Visual Studio提供的插件,提高我們的編程效率。2011-03-03
asp.net異步獲取datatable并顯示的實(shí)現(xiàn)方法
這篇文章主要介紹了asp.net異步獲取datatable并顯示的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了asp.net一步操作datatable的相關(guān)技巧,需要的朋友可以參考下2016-03-03
輕量級(jí)ORM框架Dapper應(yīng)用之安裝Dapper
這篇文章介紹了輕量級(jí)ORM框架Dapper的安裝方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03

