C#中HTML字符轉(zhuǎn)換函數(shù)分享
更新時間:2012年07月27日 09:07:39 作者:
在ASP.Net中經(jīng)常會從網(wǎng)面中取數(shù)據(jù)或更新網(wǎng)頁的顯示。因為HTML中有些特殊字符如<, >, &等,顯示實際值不一致,造成保存到數(shù)據(jù)庫再取出來時會不一樣
因此需要以下函數(shù)做轉(zhuǎn)換:
///<summary>
///替換html中的特殊字符
///</summary>
///<paramname="theString">需要進行替換的文本。</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>
///恢復html中的特殊字符
///</summary>
///<paramname="theString">需要恢復的文本。</param>
///<returns>恢復好的文本。</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;
}
復制代碼 代碼如下:
///<summary>
///替換html中的特殊字符
///</summary>
///<paramname="theString">需要進行替換的文本。</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>
///恢復html中的特殊字符
///</summary>
///<paramname="theString">需要恢復的文本。</param>
///<returns>恢復好的文本。</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)文章
ASP.NET Core 文件響應壓縮的常見使用誤區(qū)
在微軟官方文檔中,未明確指出文件壓縮功能的使用誤區(qū)。本文將對 ASP.NET Core 文件響應壓縮的常見使用誤區(qū)做出說明。2021-05-05asp.net UrlReWriter使用經(jīng)驗小結(jié)
UrlRewriter 是微軟封裝好了的一個URL重寫組件。使用它可以讓我節(jié)約很多自已開發(fā)的時間。 好了,開始講述我的應用經(jīng)驗,這只是很菜鳥的經(jīng)驗,高手就不用看了。2008-11-11將DataRow轉(zhuǎn)成指定類型的類,并返回這個類的對象(帶值)
由于實際需要 將DataRow轉(zhuǎn)成指定類型的類,并返回這個類的對象(帶值) ,實現(xiàn)方法看下面的代碼。2008-04-04Asp.Net 數(shù)據(jù)操作類(附通用數(shù)據(jù)基類)
數(shù)據(jù)操作類代碼,方便在asp.net操作數(shù)據(jù)庫2008-11-11asp.net 關(guān)于==?:和if()else()條件判斷等效例子
關(guān)于==?:和if()else() 等效例子2010-03-03