Why IndexOf returns always Zero?

牧云@^-^@ 提交于 2019-12-11 06:07:06

问题


Could someone point me why the IndexOf returns always zero in the following text?

Dim Str as string = "<p><img class=floatLeft width="330"src="http://www.com"></p><p>"
Dim Idx as integer = Str.IndexOf("<p>")

Is there any other way, of getting the index?


回答1:


Because the first occurrence of <p> is at the beginning of the string, and strings (along with many other things) are zero-indexed.

If you want to get the index of the last-occurring <p>, you can use Str.LastIndexOf("<p>").

If you want to get the index of the next-occurring <p> after the first, and assuming the string always starts with at least one <p>, you can use Str.IndexOf("<p>", "<p>".Length()) so it starts searching from after the first occurrence.


By the way, you have a syntax error in your Dim Str line, you need to escape double quotes with extra double quotes:

Dim Str as string = "<p><img class=""floatLeft"" width=""330"" src=""http://www.com""></p><p>"


来源:https://stackoverflow.com/questions/3691987/why-indexof-returns-always-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!