Zak*_*aki 0 c# string indexof backslash
我想找到一个字符串前面是否有"/",在我的代码中我得到了字符串的索引,并找出它前面的字符是否有任何东西,哪个有效,但我如何找到它实际上是正斜杠.这是我的代码:
string test = "/images/";
if (test.IndexOf(@"images/") - 1 == -1)
{
}
Run Code Online (Sandbox Code Playgroud)
编辑
我的一些字符串可能有完整的URL,有些可能是如上所述,有些可能没有/根本没有/因此使用索引
你的意思是:
if (test.StartsWith("/"))
Run Code Online (Sandbox Code Playgroud)
?(目前尚不清楚您的示例代码试图实现的目标.)
请注意,"/"是正斜杠,而不是反斜杠 - 并且在您的情况下您不需要逐字字符串文字,因为字符串不包含任何反斜杠或换行符.
编辑:你的问题不明确,但我怀疑你想要的东西:
int index = test.IndexOf(targetString);
if (index > 0 && test[index - 1] == '/')
{
// There's a leading forward slash. Deal with it appropriately
}
Run Code Online (Sandbox Code Playgroud)