C#中HTML字符转换函数
2012-07-27 13:49
309 查看
在ASP.Net中经常会从网面中取数据或更新网页的显示。因为HTML中有些特殊字符如<, >, &等,显示实际值不一致,造成保存到数据库再取出来时会不一样。因此需要以下函数做转换:
///<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;
}
相关文章推荐
- C#中HTML字符转换函数
- C#中HTML字符转换函数分享
- C#中HTML字符转换函数分享
- PHP html实体与字符之间的转换函数
- 在C#中使用正则表达式过滤html字符
- 【转】C# 过滤HTML,脚本,数据库关键字,特殊字符
- C# substring完美截取HTML字符
- C#从剪贴板获取HTML格式内容含中文字符出现的问题和解决方法
- C# 过滤HTML,脚本,数据库关键字,特殊字符
- C# 将html实体编码转换到正常字符 & #40;格式
- C# substring完美截取HTML字符
- 过滤html字符(c#)
- 过滤html字符(c#)
- C#中可以使用正则表达式来过滤html字符
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
- C#中可以使用正则表达式来过滤html字符
- 过滤html字符(c#)
- C#中关于从剪贴板中读取HTML格式含中文字符会出现的问题和解决方法
- C# 从剪贴板中读取HTML中的中文字符出现乱码问题的解决方案
- C#中使用正则表达式来过滤html字符