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

后端 未结 16 825
时光取名叫无心
时光取名叫无心 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:17

    try:

    var t = HttpUtility.JavaScriptStringEncode(s);
    
    0 讨论(0)
  • 2020-11-22 08:18

    Code:

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

    Output:

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

    Is this what you want?

    0 讨论(0)
  • 2020-11-22 08:22

    There's a method for this in Roslyn's Microsoft.CodeAnalysis.CSharp package on nuget :

        private static string ToLiteral(string valueTextForCompiler)
        {
            return Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral(valueTextForCompiler, false);
        }
    

    Obviously this didn't exist at the time of the original question, but might help people who end up here from Google.

    0 讨论(0)
  • 2020-11-22 08:23

    EDIT: A more structured approach, including all escape sequences for strings and chars.
    Doesn't replace unicode characters with their literal equivalent. Doesn't cook eggs, either.

    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('\''));
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题