Home >Backend Development >C++ >How to Convert C# Strings to Escaped String Literals?
Can the C#string value be converted into a string with a string containing a rotary sequence, such as the symbol and changing line? For example, convert the string containing the formula and the trading symbols into string containing the rotary sequence "T" and "RN"?
Answer:
Traditional method:
have been using one method for a long time:
Use this method, you can convert the following string:
<code class="language-csharp">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(); } } }</code>The output result is:
<code class="language-csharp">var input = "\tHello\r\n\tWorld!";</code>
<code>"\tHello\r\n\tWorld!"</code>The newer method involves Microsoft.Codeanalysis.csharp package from Nuget. The following is the updated code:
Using this method, you can get the same results as the previous method, and change the following string:
The output result is:
<code class="language-csharp">private static string ToLiteral(string valueTextForCompiler) { return Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral(valueTextForCompiler, false); }</code>
The above is the detailed content of How to Convert C# Strings to Escaped String Literals?. For more information, please follow other related articles on the PHP Chinese website!