2

我有一个名为的字符串fileNameArrayEdited,其中包含"\\windows". 下面if的语句没有运行。

认为问题出在其他地方,因为人们给了我应该工作的代码,一旦我发现问题就会回来......谢谢!

if (fileNameArrayEdited.StartsWith("\\"))
{
    specifiedDirCount = specifiedDirCount + 1;
}

                // Put all file names in root directory into array.
            string[] fileNameArray = Directory.GetFiles(@specifiedDir);
            int specifiedDirCount = specifiedDir.Count();
            string fileNameArrayEdited = specifiedDir.Remove(0, specifiedDirCount);
            Console.WriteLine(specifiedDir.Remove(0, specifiedDirCount));
            if (fileNameArrayEdited.StartsWith(@"\\"))
            {
                specifiedDirCount = specifiedDirCount + 1;
                Console.ReadLine();
4

2 回答 2

1

'@'如果您正在搜索正好两个斜杠,请在字符串的开头使用

if (fileNameArrayEdited.StartsWith(@"\\"))
{
  specifiedDirCount = specifiedDirCount + 1;
}

它们被称为逐字字符串,它们忽略转义字符。为了更好的解释,您可以在这里查看:http: //msdn.microsoft.com/en-us/library/362314fe.aspx

但我怀疑在这里你的一个斜线是转义字符

"\\windows"

所以你必须像这样搜索一个斜线:

if (fileNameArrayEdited.StartsWith(@"\"))
{
  specifiedDirCount = specifiedDirCount + 1;
}
于 2014-01-06T14:23:03.073 回答
0

当我们写

    string s1 = "\\" ; 
    // actual value stored in  s1 is "\"
    string s2 = @"\\" ; 
    // actual value stored in  s2 is "\\"

第二种类型的string(s) 称为“逐字”字符串。

于 2014-01-06T14:27:09.967 回答