检查一个字符串是否包含另一个字符串 [英] Check if a string contains another string
问题描述
我想查找字符串中是否包含,"(逗号).除了逐个字符读取之外,我们还有其他选择吗?
I want to find if a string contains a ","(comma) in it. Do we have any other option other than reading char-by-char?
推荐答案
使用 Instr 函数
Dim pos As Integer
pos = InStr("find the comma, in the string", ",")
将在 pos 中返回 15
will return 15 in pos
如果没有找到则返回0
如果您需要使用 Excel 公式查找逗号,您可以使用 =FIND(",";A1)
函数.
If you need to find the comma with an excel formula you can use the =FIND(",";A1)
function.
请注意,如果您想使用 Instr
来查找不区分大小写的字符串的位置,请使用 Instr 的第三个参数并为其指定 const vbTextCompare
(或只是1 为顽固分子).
Notice that if you want to use Instr
to find the position of a string case-insensitive use the third parameter of Instr and give it the const vbTextCompare
(or just 1 for die-hards).
Dim posOf_A As Integer
posOf_A = InStr(1, "find the comma, in the string", "A", vbTextCompare)
会给你一个 14 的值.
will give you a value of 14.
请注意,在这种情况下,您必须按照我链接的规范中的说明指定开始位置:如果指定了比较,则需要开始参数.
Note that you have to specify the start position in this case as stated in the specification I linked: The start argument is required if compare is specified.
这篇关于检查一个字符串是否包含另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!