在双引号和空字符串之间替换\ n或\ r或\ r \ n的最佳方法是什么
使用c#?
示例:" abc \ ndef"应该是" abcdef"
答案 0 :(得分:0)
不确定"最佳",但这可行:
string a = "abc\nefg";
a = string.Concat(a.Split(new char[] { '\n', '\r' }));
你能用Best来定义你的意思吗?最快的 ?少代码?可读吗?
答案 1 :(得分:0)
这样的事情:
public static String RemoveQuots(String source) {
if (String.IsNullOrEmpty(source))
return source;
StringBuilder sb = new StringBuilder(source.Length);
Boolean inQuot = false;
foreach (var ch in source) {
if (ch == '"')
inQuot = !inQuot;
if (!inQuot || ((ch != '\n') && (ch != '\r')))
sb.Append(ch);
}
return sb.ToString();
}
...
String source = "\"abc\ndef\"";
String result = RemoveQuots(source);
详细测试
String source = "preserved: \n \"deleted: \n\" \"\" preserved: \n tail";
// preserved:
// "deleted: " "" preserved:
// tail
String result = RemoveQuots(source);
说明:
1st \n is out double quots
2nd \n is within double quotes: \"deleted: \n\" (note \")
3d \"\" is just empty "" *string* so \n is once again doomed to be deleted
答案 2 :(得分:0)