如何计算字符串中反斜杠的个数?
我尝试了以下方法,但都不起作用。
string s = @"\a\a\n\u\u0013((((\a\b\n"; // output must be 8
int count = s.Count(a => a == "\\"); // Operator == cant be applied of type char & string
int count = s.Count(a => a == "\"); // newline in constant
int count = s.Split('\\').Length // it doesnt count
发布于 2017-07-10 15:23:04
您的第一次尝试几乎是正确的;但是您需要比较字符和字符,而不是字符和字符串。
你的代码应该是:
int count = s.Count(a => a == '\\');
https://stackoverflow.com/questions/45005963
复制相似问题