string testStr="thestringhasa\slash";
if(testStr.Contains("\"))
{
//Code to process string with \
}
当我尝试使用if语句检测一个字符串是否包含反斜杠时,应该如何正确测试?但是当我尝试使用if语句时,它会显示常量中有一个新行。
使用字符串字面量符号@。在@符号前的字符串告诉C#编译器将字符串视为文字而不转义任何内容。
使用转义字符来告诉C#编译器有一个特殊字符实际上是字符串的一部分。
var temp1 = @"test\test";
var test2 = "test\\test";
test1 == test2; // Yields true
你应该使用双斜杠
string testStr=@"thestringhasa\slash";
if(testStr.Contains("\\"))
{
//Code to process string with \
}
\\
string testStr = @"thestringhasa\slash";
if (testStr.Contains("\\"))
{
//Code to process string with \
}
\\\
以进行适当的测试。或者使用字符串 testStr=@"thestringhasa\slash"; - va.