我跳進這個意外並沒有任何線索,爲什麼發生這種情況初始化字符串似乎包含的String.Empty
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 0
10
11
我很滿意最後一部分,但我不知道爲什麼這被評估爲真。即使Indexof
和LastIndexOf
也有不同的值。
任何人都可以幫我解決這是爲什麼?
編輯
我相信這是一個有點相關的我的問題,並也將是幫助那些在這個問題誰絆倒。
看到這個SO鏈接:Why does "abcd".StartsWith("") return true?
@ SwDevMan81:應該不是11而不是10? – V4Vendetta 2011-05-04 11:50:12
@ V4Vendetta - 最後的索引是10(0到10是11個字符) – SwDevMan81 2011-05-04 11:51:17
@ SwDevMan81:對不起,我的錯誤沒有讀取'index',所以它暗示了對於上面給出的任何字符串string.Empty將始終解析爲true (很高興知道是否有例外情況) – V4Vendetta 2011-05-04 11:56:03