Can I convert a C# string value to an escaped string literal

后端 未结 16 1012
时光取名叫无心
时光取名叫无心 2020-11-22 07:51

In C#, can I convert a string value to a string literal, the way I would see it in code? I would like to replace tabs, newlines, etc. with their escape sequences.

If

16条回答
  •  没有蜡笔的小新
    2020-11-22 08:00

    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();
      }
    }
    

提交回复
热议问题