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:
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!";
<code>"\tHello\r\n\tWorld!"</code>
Using this method, you can get the same results as the previous method, and change the following string:
The output result is:
private static string ToLiteral(string valueTextForCompiler) { return Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral(valueTextForCompiler, false); }
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!