This question already has answers here:
Why does “abcd”.StartsWith(“”) return true?

(11个答案)


已关闭8年。




我不小心跳了进去,不知道为什么会这样
string sample = "Hello World";
if (sample.Contains(string.Empty))
{
    Console.WriteLine("This contains an empty part ? at position " + sample.IndexOf(string.Empty));
    Console.WriteLine(sample.LastIndexOf(string.Empty));
    Console.WriteLine(sample.Length);
}

输出
This contains an empty part ? at position 01011
我对最后一部分感到满意​​,但是我不知道为什么将其评估为正确。甚至IndexofLastIndexOf也有单独的值。

谁能帮助我,为什么会这样呢?

编辑

我认为这与我的问题有点相关,也将对那些偶然发现此问题的人有所帮助。

看到这个SO链接:Why does "abcd".StartsWith("") return true?

最佳答案

从msdn获取IndexOf



对于LastIndexOf



对于Contains

关于c# - 初始化的字符串似乎包含string.Empty,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5882818/

10-09 07:13
查看更多