1
有人能指出爲什麼IndexOf在下面的文本中總是返回零嗎?爲什麼IndexOf總是返回零?
Dim Str as string = "<p><img class=floatLeft width="330"src="http://www.com"></p><p>"
Dim Idx as integer = Str.IndexOf("<p>")
是否有任何其他方式獲取索引?
有人能指出爲什麼IndexOf在下面的文本中總是返回零嗎?爲什麼IndexOf總是返回零?
Dim Str as string = "<p><img class=floatLeft width="330"src="http://www.com"></p><p>"
Dim Idx as integer = Str.IndexOf("<p>")
是否有任何其他方式獲取索引?
因爲第一個<p>
的出現位於字符串的開頭,並且字符串(以及許多其他事物)都是零索引的。
如果你想得到的最後的索引,可以使用Str.LastIndexOf("<p>")
。
如果你想獲得的下一個後的第一個-occurring <p>
,並假設該字符串索引總是開始有至少一個<p>
,你可以使用Str.IndexOf("<p>", "<p>".Length())
所以它開始從第一次出現後,搜索。
順便說一句,你有你的Dim Str
行了語法錯誤,你需要轉義雙引號有額外的雙引號:
Dim Str as string = "<p><img class=""floatLeft"" width=""330"" src=""http://www.com""></p><p>"