在Java中,我们可以使用string.matches("。* anytext。*")来查看字符串是否包含字符串' anytext'在其中,如何在Visual Basic for Excel中执行相同操作?
答案 0 :(得分:1)
您可以将Like
运算符与通配符符号*
一起使用:
MyString Like "*anytext*"
当且仅当MyString
包含"anytext"
时,才会返回true。
有关详细信息,请参阅this。
答案 1 :(得分:0)
使用Instr功能
Dim pos As Integer
pos = InStr("find the comma, in the string", ",")
将在pos中返回15
如果没有找到它将返回0
如果您需要使用excel公式找到逗号,可以使用=FIND(",";A1)
函数。
请注意,如果您想使用Instr
查找不区分大小写的字符串的位置,请使用Instr
的第三个参数并为其指定const vbTextCompare
(或仅为1顽固派)。
Dim posOf_A As Integer
posOf_A = InStr(1, "find the comma, in the string", "A", vbTextCompare)
会给你14的值。
请注意,您必须在此情况下指定起始位置,如我链接的规范中所述:如果指定了compare,则需要start参数。