首页 > 解决方案 > 检查字符串是否包含在另一个字符串中

问题描述

我的一根弦有问题,我很想得到一些帮助。通过迭代,我得到了那些 htmlelement innertex:

31-12-2017

31-12-2016

2015 年 31 月 12 日等

我希望在这些元素中搜索通过输入框插入的一个字符串,但该字符串仅包含年份(例如 2017 年)。我尝试使用 instr 和 instrrev 并且在这两种情况下,即使存在字符串也不匹配。

这是我的代码的一部分:

Dim setdata as String
Dim datacheck as mshtml.ihtmlelement
Dim datachecks as mshtml.ihtmlelementCollection

Setdata = application.inputbox 'etc.

Datachecks = iedoc.getelementbyclassname("example") 'it finds all the date in the page (I checked through debug.print)'

For each datacheck in datachecks:
If instr(setdata, datacheck.innertext, vbtextcompare) then 'i also tried instrrev'
Debug.print "ok"
Else
Debug.print "no"
End if
Next datacheck

即使通过输入框插入 2017 和元素 collection.contains 31-12-2017,我也总是没有。你能帮助我吗?

标签: vbaloops

解决方案


你是不是混淆了顺序。对我来说,您的代码似乎正在“setdata”中寻找“datacheck”,而我的理解是您正在“datacheck”中寻找“setdata”。

Dim a as String         'The string to look for
Dim b as String         'The string to look in
Dim c as Long

a = "A"
b = "aabb AbAb BAAb"

c = InStr(b,a)         'Function looking for the first occurrence of a in b
Print.Debug c          'This will give the value of 6, the location "A" first appears in string b.

BR FNK


推荐阅读