\ nまたは\ rのかを交換するための最良の方法は何で一部の文字を置換するには、\ rを\ nは、彼らは、C#を使用して、空の文字列どのように文字列
で、二重引用符の間にあるとき?
例: "ABC \ NDEF" その "最高" の場合
\ nまたは\ rのかを交換するための最良の方法は何で一部の文字を置換するには、\ rを\ nは、彼らは、C#を使用して、空の文字列どのように文字列
で、二重引用符の間にあるとき?
例: "ABC \ NDEF" その "最高" の場合
このような何か:
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
ありがとう、これは私が探していた正確な答えです! – nice
わからない "ABCDEF" である必要がありますが、これは動作します:
string a = "abc\nefg";
a = string.Concat(a.Split(new char[] { '\n', '\r' }));
は、あなたがベストで何を意味するかを定義することはできますか?最速?少ないコード?読みやすい?
あなたは何を試してみましたか? 'String.Replace'を試しましたか? "最高"を定義する二重引用符は何と関係していますか?置き換えてはいけない二重引用符の外側に改行がありますか? –
"abc \ ndef" \ n "ghi \ n"を持っていれば、引用符内の改行を置き換える必要があり、二重引用符の外側は – nice
のままにしておく必要があります。\ n " ghi " – nice