为什么IndexOf返回始终为零? [英] Why IndexOf returns always Zero?

查看:1003
本文介绍了为什么IndexOf返回始终为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指出为什么在下面的文本中IndexOf总是返回零吗?

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?

推荐答案

因为第一个 出现在字符串的开头,所以字符串(以及许多其他东西)位于字符串的开头零索引.

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

如果要获取最后出现的<p>的索引,可以使用Str.LastIndexOf("<p>").

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

如果要获取在第一个之后的 next 出现的<p>的索引,并假设字符串总是以至少一个<p>开头,您可以使用Str.IndexOf("<p>", "<p>".Length()),以便它从第一次出现后开始搜索.

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.

顺便说一句,您在Dim Str行中存在语法错误,您需要使用双引号将双引号转义:

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>"

这篇关于为什么IndexOf返回始终为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆