C# 基于StartsWith not finding的If语句\\&引用;在C中#
我有一个名为C# 基于StartsWith not finding的If语句\\&引用;在C中#,c#,if-statement,startswith,C#,If Statement,Startswith,我有一个名为filenamearayeedited的字符串,其中包含“\\windows”。如果语句未运行,则下面的 认为问题出在别人给我的代码中,一旦我发现问题,应该可以工作的代码就会回来。。。谢谢 if (fileNameArrayEdited.StartsWith("\\")) { specifiedDirCount = specifiedDirCount + 1; } // Put all file names in root directory
filenamearayeedited
的字符串,其中包含“\\windows”
。如果语句未运行,则下面的
认为问题出在别人给我的代码中,一旦我发现问题,应该可以工作的代码就会回来。。。谢谢强>
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();
如果要搜索正好两个斜杠,请在字符串开头使用'@'
if (fileNameArrayEdited.StartsWith(@"\\"))
{
specifiedDirCount = specifiedDirCount + 1;
}
它们被称为逐字字符串,并且忽略转义字符。要获得更好的解释,您可以查看以下内容:
但我怀疑在这里,你的一条斜线就是逃逸字符
"\\windows"
因此,您必须搜索一条斜线,如下所示:
if (fileNameArrayEdited.StartsWith(@"\"))
{
specifiedDirCount = specifiedDirCount + 1;
}
当我们写作时
string s1 = "\\" ;
// actual value stored in s1 is "\"
string s2 = @"\\" ;
// actual value stored in s2 is "\\"
第二种类型的字符串称为“逐字”字符串 @user3165670您可以发布您的字符串吗?请添加一个标记,指明这是什么语言是的,哪种编程语言?字符串是用户定义的,语言是c#,很抱歉。请注意,“\\”
检查是否有一个反斜杠。使用“\\\\”
或@“\\\”
作为双精度。我怀疑你是对的,但你可能想给出一些解释!它看起来应该对我有用,但我无法让它触发if语句。与我之前的评论相反,我的想法是StartsWith(“\\”)表示一个斜杠。。。哪个@\\Windows有。。。应该如此match@Kiku-Suma您确定您的字符串是从\\windows开始的还是只包含\\windows?@Selman22当用户在字符串中输入\windows时,我认为这可能是问题发生的地方。正确,但是如果您查看代码\\确实以\开始,因此无论如何,它都应该返回true@Liath我怀疑您正在检查\\,而您应该只检查\项