Home >Backend Development >C++ >How to Escape Strings for C# String Literals?
Convert the ordinary string to its righteous string representing form, you can consider the following methods:
Use code to generate
<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>This method follows the following steps to create a facade string representation form:
<code class="language-csharp">private static string ToLiteral(string valueTextForCompiler) { return Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral(valueTextForCompiler, false); }</code>This method generates the procedure by using Roslyn's Symboldisplay to generate the string of the righteousness, simplifying the process.
The above is the detailed content of How to Escape Strings for C# String Literals?. For more information, please follow other related articles on the PHP Chinese website!