Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

string - 检查字符串是否包含另一个字符串(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?

(除了阅读char-by-char之外,我们还有其他选择吗?)

  ask by krishna translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the Instr function

(使用Instr功能)

Dim pos As Integer

pos = InStr("find the comma, in the string", ",")

will return 15 in pos

(将返回15 in pos)

If not found it will return 0

(如果没有找到它将返回0)

If you need to find the comma with an excel formula you can use the =FIND(",";A1) function.

(如果需要使用excel公式查找逗号,可以使用=FIND(",";A1)函数。)

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).

(请注意,如果要使用Instr来查找不区分大小写的字符串的位置,请使用Instr的第三个参数,并为其提供const vbTextCompare (或者仅为1表示顽固分子)。)

Dim posOf_A As Integer

posOf_A = InStr(1, "find the comma, in the string", "A", vbTextCompare)

will give you a value of 14.

(会给你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.

(请注意,在这种情况下,您必须指定起始位置,如我链接的规范中所述: 如果指定了compare,则需要start参数。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...