我可以将C#string值转换为转义的string文字

在C#中,我可以将string值转换为string文字,我将在代码中看到它? 我想用他们的转义序列replace制表符,换行符等。

如果这个代码:

Console.WriteLine(someString); 

生产:

 Hello World! 

我想要这个代码:

 Console.WriteLine(ToLiteral(someString)); 

生产:

 \tHello\r\n\tWorld!\r\n 

我find了这个:

 private static string ToLiteral(string input) { using (var writer = new StringWriter()) { using (var provider = CodeDomProvider.CreateProvider("CSharp")) { provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null); return writer.ToString(); } } } 

此代码:

 var input = "\tHello\r\n\tWorld!"; Console.WriteLine(input); Console.WriteLine(ToLiteral(input)); 

生产:

  Hello World! "\tHello\r\n\tWorld!" 

Regex.Escape(String)呢?

Regex.Escape通过用它们的转义代码replace它们来转义一组最小的字符(\,*,+,?,|,{,[,(,),^,$,。,#和空格)。

编辑:一个更结构化的方法,包括string s和string的所有转义序列。
不能用它们的字面值代替unicode字符。 不要煮鸡蛋。

 public class ReplaceString { static readonly IDictionary<string, string> m_replaceDict = new Dictionary<string, string>(); const string ms_regexEscapes = @"[\a\b\f\n\r\t\v\\""]"; public static string StringLiteral(string i_string) { return Regex.Replace(i_string, ms_regexEscapes, match); } public static string CharLiteral(char c) { return c == '\'' ? @"'\''" : string.Format("'{0}'", c); } private static string match(Match m) { string match = m.ToString(); if (m_replaceDict.ContainsKey(match)) { return m_replaceDict[match]; } throw new NotSupportedException(); } static ReplaceString() { m_replaceDict.Add("\a", @"\a"); m_replaceDict.Add("\b", @"\b"); m_replaceDict.Add("\f", @"\f"); m_replaceDict.Add("\n", @"\n"); m_replaceDict.Add("\r", @"\r"); m_replaceDict.Add("\t", @"\t"); m_replaceDict.Add("\v", @"\v"); m_replaceDict.Add("\\", @"\\"); m_replaceDict.Add("\0", @"\0"); //The SO parser gets fooled by the verbatim version //of the string to replace - @"\""" //so use the 'regular' version m_replaceDict.Add("\"", "\\\""); } static void Main(string[] args){ string s = "here's a \"\n\tstring\" to test"; Console.WriteLine(ReplaceString.StringLiteral(s)); Console.WriteLine(ReplaceString.CharLiteral('c')); Console.WriteLine(ReplaceString.CharLiteral('\'')); } } 
 public static class StringHelpers { private static Dictionary<string, string> escapeMapping = new Dictionary<string, string>() { {"\"", @"\\\"""}, {"\\\\", @"\\"}, {"\a", @"\a"}, {"\b", @"\b"}, {"\f", @"\f"}, {"\n", @"\n"}, {"\r", @"\r"}, {"\t", @"\t"}, {"\v", @"\v"}, {"\0", @"\0"}, }; private static Regex escapeRegex = new Regex(string.Join("|", escapeMapping.Keys.ToArray())); public static string Escape(this string s) { return escapeRegex.Replace(s, EscapeMatchEval); } private static string EscapeMatchEval(Match m) { if (escapeMapping.ContainsKey(m.Value)) { return escapeMapping[m.Value]; } return escapeMapping[Regex.Escape(m.Value)]; } } 

尝试:

 var t = HttpUtility.JavaScriptStringEncode(s); 

完整的工作实现,包括Unicode和ASCII不可打印字符的转义。 不要插入“+”符号,如Hallgrim的答案 。

  static string ToLiteral(string input) { StringBuilder literal = new StringBuilder(input.Length + 2); literal.Append("\""); foreach (var c in input) { switch (c) { case '\'': literal.Append(@"\'"); break; case '\"': literal.Append("\\\""); break; case '\\': literal.Append(@"\\"); break; case '\0': literal.Append(@"\0"); break; case '\a': literal.Append(@"\a"); break; case '\b': literal.Append(@"\b"); break; case '\f': literal.Append(@"\f"); break; case '\n': literal.Append(@"\n"); break; case '\r': literal.Append(@"\r"); break; case '\t': literal.Append(@"\t"); break; case '\v': literal.Append(@"\v"); break; default: // ASCII printable character if (c >= 0x20 && c <= 0x7e) { literal.Append(c); // As UTF16 escaped character } else { literal.Append(@"\u"); literal.Append(((int)c).ToString("x4")); } break; } } literal.Append("\""); return literal.ToString(); } 

Hallgrim的回答非常好,但是“+”,换行符和缩进添加function对我来说已经不起作用了。 一个简单的方法是:

 private static string ToLiteral(string input) { using (var writer = new StringWriter()) { using (var provider = CodeDomProvider.CreateProvider("CSharp")) { provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, new CodeGeneratorOptions {IndentString = "\t"}); var literal = writer.ToString(); literal = literal.Replace(string.Format("\" +{0}\t\"", Environment.NewLine), ""); return literal; } } } 

有趣的问题。

如果找不到更好的方法,可以随时更换。
如果你select它,你可以使用这个C#转义序列列表

  • 单引号,字符文字需要
  • \“ – 双引号,需要string文字
  • \ – 反斜杠
  • \ 0 – Unicode字符0
  • \警报(字符7)
  • \ b – 退格(字符8)
  • \ f – 换页(字符12)
  • \ n – 新行(字符10)
  • \ r – 回车(字符13)
  • \ t – 水平制表符(字符9)
  • \ v – 垂直报价(字符11)
  • \ uxxxx – hex值为xxxx的字符的Unicode转义序列
  • \ xn [n] [n] [n] – hex值为nnnn的字符的Unicode转义序列(\ uxxxx的可变长度版本)
  • \ Uxxxxxxxx – hex值为xxxxxxxx的字符的Unicode转义序列(用于生成代理)

由于原来的链接不可用,这个列表被转换为这个答案:
C#Escape序列的列表

完整的文章仍然可以通过Wayback机器访问。

对于Smilediver的回答,这是一个小改进,它不会逃避所有没有ASCII码的字符,但只有这些才是真正需要的。

 using System; using System.Globalization; using System.Text; public static class CodeHelper { public static string ToLiteral(this string input) { var literal = new StringBuilder(input.Length + 2); literal.Append("\""); foreach (var c in input) { switch (c) { case '\'': literal.Append(@"\'"); break; case '\"': literal.Append("\\\""); break; case '\\': literal.Append(@"\\"); break; case '\0': literal.Append(@"\0"); break; case '\a': literal.Append(@"\a"); break; case '\b': literal.Append(@"\b"); break; case '\f': literal.Append(@"\f"); break; case '\n': literal.Append(@"\n"); break; case '\r': literal.Append(@"\r"); break; case '\t': literal.Append(@"\t"); break; case '\v': literal.Append(@"\v"); break; default: if (Char.GetUnicodeCategory(c) != UnicodeCategory.Control) { literal.Append(c); } else { literal.Append(@"\u"); literal.Append(((ushort)c).ToString("x4")); } break; } } literal.Append("\""); return literal.ToString(); } } 
 public static class StringEscape { static char[] toEscape = "\0\x1\x2\x3\x4\x5\x6\a\b\t\n\v\f\r\xe\xf\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\"\\".ToCharArray(); static string[] literals = @"\0,\x0001,\x0002,\x0003,\x0004,\x0005,\x0006,\a,\b,\t,\n,\v,\f,\r,\x000e,\x000f,\x0010,\x0011,\x0012,\x0013,\x0014,\x0015,\x0016,\x0017,\x0018,\x0019,\x001a,\x001b,\x001c,\x001d,\x001e,\x001f".Split(new char[] { ',' }); public static string Escape(this string input) { int i = input.IndexOfAny(toEscape); if (i < 0) return input; var sb = new System.Text.StringBuilder(input.Length + 5); int j = 0; do { sb.Append(input, j, i - j); var c = input[i]; if (c < 0x20) sb.Append(literals[c]); else sb.Append(@"\").Append(c); } while ((i = input.IndexOfAny(toEscape, j = ++i)) > 0); return sb.Append(input, j, input.Length - j).ToString(); } } 

我试图在Hallgrim上面接受的答案中joinToVerbatim:

 private static string ToLiteral(string input) { using (var writer = new StringWriter()) { using (var provider = CodeDomProvider.CreateProvider("CSharp")) { provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, new CodeGeneratorOptions { IndentString = "\t" }); var literal = writer.ToString(); literal = literal.Replace(string.Format("\" +{0}\t\"", Environment.NewLine), ""); return literal; } } } private static string ToVerbatim( string input ) { string literal = ToLiteral( input ); string verbatim = "@" + literal.Replace( @"\r\n", Environment.NewLine ); return verbatim; } 

我提交我自己的实现,它处理null值,应该更多的性能考虑使用数组查询表,手动hex转换,并避免switch语句。

 using System; using System.Text; using System.Linq; public static class StringLiteralEncoding { private static readonly char[] HEX_DIGIT_LOWER = "0123456789abcdef".ToCharArray(); private static readonly char[] LITERALENCODE_ESCAPE_CHARS; static StringLiteralEncoding() { // Per http://msdn.microsoft.com/en-us/library/h21280bw.aspx var escapes = new string[] { "\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", "\"\"", "\\\\", "??", "\00" }; LITERALENCODE_ESCAPE_CHARS = new char[escapes.Max(e => e[0]) + 1]; foreach(var escape in escapes) LITERALENCODE_ESCAPE_CHARS[escape[0]] = escape[1]; } /// <summary> /// Convert the string to the equivalent C# string literal, enclosing the string in double quotes and inserting /// escape sequences as necessary. /// </summary> /// <param name="s">The string to be converted to a C# string literal.</param> /// <returns><paramref name="s"/> represented as a C# string literal.</returns> public static string Encode(string s) { if(null == s) return "null"; var sb = new StringBuilder(s.Length + 2).Append('"'); for(var rp = 0; rp < s.Length; rp++) { var c = s[rp]; if(c < LITERALENCODE_ESCAPE_CHARS.Length && '\0' != LITERALENCODE_ESCAPE_CHARS[c]) sb.Append('\\').Append(LITERALENCODE_ESCAPE_CHARS[c]); else if('~' >= c && c >= ' ') sb.Append(c); else sb.Append(@"\x") .Append(HEX_DIGIT_LOWER[c >> 12 & 0x0F]) .Append(HEX_DIGIT_LOWER[c >> 8 & 0x0F]) .Append(HEX_DIGIT_LOWER[c >> 4 & 0x0F]) .Append(HEX_DIGIT_LOWER[c & 0x0F]); } return sb.Append('"').ToString(); } } 

码:

 string someString1 = "\tHello\r\n\tWorld!\r\n"; string someString2 = @"\tHello\r\n\tWorld!\r\n"; Console.WriteLine(someString1); Console.WriteLine(someString2); 

输出:

  Hello World! \tHello\r\n\tWorld!\r\n 

这是你想要的吗?