asp html转义字符,C# ASP.NET 中html常见转义字符的处理

C# ASP.NET 中html常见转义字符的处理

using System;

namespace ConsoleApp31

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(HtmlToEsc("有 空 格"));

Console.WriteLine(EscToHtml("有   空     格"));

Console.Read();

}

///

/// Html to Esc

///

/// input

///

public static string HtmlToEsc(string input)

{

if (string.IsNullOrEmpty(input)) { return ""; }

input = input.Replace("&", "&")

.Replace("'", "'")

.Replace("\"", """)

.Replace("

.Replace(">", ">")

.Replace(" ", " ")

.Replace("©", "©")

.Replace("®", "®")

.Replace("™", "™");

return input;

}

///

/// Esc to Html

///

/// input

///

public static string EscToHtml(string input)

{

if (string.IsNullOrEmpty(input)) { return ""; }

input = input.Replace("™", "™")

.Replace("®", "®")

.Replace("©", "©")

.Replace(" ", " ")

.Replace(">", ">")

.Replace("<", "

.Replace(""", "\"")

.Replace("'", "'")

.Replace("&", "&");

return input;

}

}

}