c# 如何使用反斜杠拆分字符串(双斜杠不起作用)

c# How to split a string using a back slash (double slash not working)

我正在尝试使用 '\' 拆分字符串。

我已阅读主题 How to split using a back slash,其中有使用转义字符 '\\' 而不是 '\ 的好建议'Split 方法中。

但是,如果我使用 '\\',这个 "eating" 我想要拆分的单词的第一个符号。

这是我的代码:

        string firstString = "one\two\three";
        char a = '\';
        string[] splittedString = firstString.Split(a);
        foreach (string s in splittedString)
        {
            Console.WriteLine(s);
        }

//输出为"one wo hree"

为什么?我的错误在哪里?

尝试重写

string firstString = "one\two\three";

您要么需要像这样转义 firstString 中的 \

string firstString = "one\two\three";

或者像这样用“@”作为前缀

string firstString = @"one\two\three";

这些可能会有所帮助 https://blogs.msdn.microsoft.com/csharpfaq/2004/03/12/what-character-escape-sequences-are-available/ and http://www.yoda.arachsys.com/csharp/strings.html