我有以下代码
string line = "";
while ((line = stringReader.ReadLine()) != null)
{
// split the lines
for (int c = 0; c < line.Length; c++)
{
if ( line[c] == ',' && line[c - 1] == '"' && line[c + 1] == '"')
{
line.Trim(new char[] {'\\'}); // <------
lineBreakOne = line.Substring(1, c - 2);
lineBreakTwo = line.Substring(c + 2, line.Length - 2);
}
}
}
我在想要移除字符串中所有的 '\' 字符,为此已经在这一行添加了注释。这是正确的做法吗?但是它没有生效,所有的 '\' 仍然存在于字符串中。
Trim(new char[] {'\\'})
将从开头或结尾删除所有 \ 字符。它会将它们“修剪”掉。正如 @user978511 所述,您可以使用Replace("\\", "")
。 (FYI,他使用 @ 字符表示“按照字面意思处理此字符串,而不应用转义规则”) - JohnL