我可以将 C# 字符串值转换为转义的字符串文字吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/323640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I convert a C# string value to an escaped string literal
提问by Hallgrim
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.
在 C# 中,我可以将字符串值转换为字符串文字,就像我在代码中看到的那样吗?我想用它们的转义序列替换制表符、换行符等。
If this code:
如果这段代码:
Console.WriteLine(someString);
produces:
产生:
Hello
World!
I want this code:
我想要这个代码:
Console.WriteLine(ToLiteral(someString));
to produce:
生产:
\tHello\r\n\tWorld!\r\n
采纳答案by Hallgrim
I found this:
我找到了这个:
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();
}
}
}
This code:
这段代码:
var input = "\tHello\r\n\tWorld!";
Console.WriteLine(input);
Console.WriteLine(ToLiteral(input));
Produces:
产生:
Hello
World!
"\tHello\r\n\tWorld!"
回答by Nelson Reis
Interesting question.
有趣的问题。
If you can't find a better method, you can always replace.
In case you're opting for it, you could use this C# Escape Sequence List:
如果你找不到更好的方法,你可以随时更换。
如果你选择它,你可以使用这个C# 转义序列列表:
- \' - single quote, needed for character literals
- \" - double quote, needed for string literals
- \ - backslash
- \0 - Unicode character 0
- \a - Alert (character 7)
- \b - Backspace (character 8)
- \f - Form feed (character 12)
- \n - New line (character 10)
- \r - Carriage return (character 13)
- \t - Horizontal tab (character 9)
- \v - Vertical quote (character 11)
- \uxxxx - Unicode escape sequence for character with hex value xxxx
- \xn[n][n][n] - Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)
- \Uxxxxxxxx - Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)
- \' - 单引号,字符文字需要
- \" - 双引号,字符串文字需要
- \ - 反斜杠
- \0 - Unicode 字符 0
- \a - 警报(字符 7)
- \b - 退格(字符 8)
- \f - 换页(字符 12)
- \n - 新行(字符 10)
- \r - 回车(字符 13)
- \t - 水平制表符(字符 9)
- \v - 垂直引号(字符 11)
- \uxxxx - 具有十六进制值 xxxx 的字符的 Unicode 转义序列
- \xn[n][n][n] - 具有十六进制值 nnnn 的字符的 Unicode 转义序列(\uxxxx 的可变长度版本)
- \Uxxxxxxxx - 具有十六进制值 xxxxxxxx 的字符的 Unicode 转义序列(用于生成代理)
This list can be found in the C# Frequently Asked Questions What character escape sequences are available?
此列表可在 C# 常见问题解答中找到 哪些字符转义序列可用?
回答by Cristian Diaconescu
EDIT: A more structured approach, including all escape sequences for string
s and char
s.
Doesn't replace unicode characters with their literal equivalent.
Doesn't cook eggs, either.
编辑:一种更结构化的方法,包括string
s 和char
s 的所有转义序列。
不将 unicode 字符替换为其文字等效项。也不会煮鸡蛋。
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("string someString1 = "\tHello\r\n\tWorld!\r\n";
string someString2 = @"\tHello\r\n\tWorld!\r\n";
Console.WriteLine(someString1);
Console.WriteLine(someString2);
", @" Hello
World!
\tHello\r\n\tWorld!\r\n
");
//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('\''));
}
}
回答by rfgamaral
Code:
代码:
public static class StringHelpers
{
private static Dictionary<string, string> escapeMapping = new Dictionary<string, string>()
{
{"\"", @"\\"""},
{"\\", @"\"},
{"\a", @"\a"},
{"\b", @"\b"},
{"\f", @"\f"},
{"\n", @"\n"},
{"\r", @"\r"},
{"\t", @"\t"},
{"\v", @"\v"},
{"var t = HttpUtility.JavaScriptStringEncode(s);
", @" static string ToLiteral(string input) {
StringBuilder literal = new StringBuilder(input.Length + 2);
literal.Append("\"");
foreach (var c in input) {
switch (c) {
case '\'': literal.Append(@"\'"); break;
case '\"': literal.Append("\\""); break;
case '\': literal.Append(@"\"); break;
case 'using System;
using System.Globalization;
using System.Text;
public static class CodeHelper
{
public static string ToLiteral(this string input)
{
var literal = new StringBuilder(input.Length + 2);
literal.Append("\"");
foreach (var c in input)
{
switch (c)
{
case '\'': literal.Append(@"\'"); break;
case '\"': literal.Append("\\""); break;
case '\': literal.Append(@"\"); break;
case 'private static string ToLiteral(string input)
{
using (var writer = new StringWriter())
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, new CodeGeneratorOptions {IndentString = "\t"});
var literal = writer.ToString();
literal = literal.Replace(string.Format("\" +{0}\t\"", Environment.NewLine), "");
return literal;
}
}
}
': literal.Append(@"##代码##"); break;
case '\a': literal.Append(@"\a"); break;
case '\b': literal.Append(@"\b"); break;
case '\f': literal.Append(@"\f"); break;
case '\n': literal.Append(@"\n"); break;
case '\r': literal.Append(@"\r"); break;
case '\t': literal.Append(@"\t"); break;
case '\v': literal.Append(@"\v"); break;
default:
if (Char.GetUnicodeCategory(c) != UnicodeCategory.Control)
{
literal.Append(c);
}
else
{
literal.Append(@"\u");
literal.Append(((ushort)c).ToString("x4"));
}
break;
}
}
literal.Append("\"");
return literal.ToString();
}
}
': literal.Append(@"##代码##"); break;
case '\a': literal.Append(@"\a"); break;
case '\b': literal.Append(@"\b"); break;
case '\f': literal.Append(@"\f"); break;
case '\n': literal.Append(@"\n"); break;
case '\r': literal.Append(@"\r"); break;
case '\t': literal.Append(@"\t"); break;
case '\v': literal.Append(@"\v"); break;
default:
// ASCII printable character
if (c >= 0x20 && c <= 0x7e) {
literal.Append(c);
// As UTF16 escaped character
} else {
literal.Append(@"\u");
literal.Append(((int)c).ToString("x4"));
}
break;
}
}
literal.Append("\"");
return literal.ToString();
}
"},
};
private static Regex escapeRegex = new Regex(string.Join("|", escapeMapping.Keys.ToArray()));
public static string Escape(this string s)
{
return escapeRegex.Replace(s, EscapeMatchEval);
}
private static string EscapeMatchEval(Match m)
{
if (escapeMapping.ContainsKey(m.Value))
{
return escapeMapping[m.Value];
}
return escapeMapping[Regex.Escape(m.Value)];
}
}
Output:
输出:
##代码##Is this what you want?
这是你想要的吗?
回答by ICR
回答by Arsen Zahray
try:
尝试:
##代码##回答by Smilediver
Fully working implementation, including escaping of Unicode and ASCII non printable characters. Does not insert "+" signs like Hallgrim's answer.
完整的工作实现,包括转义 Unicode 和 ASCII 不可打印字符。不会像Hallgrim's answer那样插入“+”号。
##代码##回答by Shqdooow
What about Regex.Escape(String)?
Regex.Escape escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes.
Regex.Escape 通过用转义码替换它们来转义最少的一组字符(\、*、+、?、|、{、[、(、)、^、$、.、# 和空格)。
回答by deerchao
Here is a little improvement for Smilediver's answer, it will not escape all no-ASCII chars but only these are really needed.
这是 Smilediver 的答案的一些改进,它不会转义所有非 ASCII 字符,但只有这些才是真正需要的。
##代码##回答by lesur
Hallgrim's answer is excellent, but the "+", newline and indent additions were breaking functionality for me. An easy way around it is:
Hallgrim 的回答非常好,但是“+”、换行符和缩进添加对我来说破坏了功能。一个简单的方法是:
##代码##