检查字符串是否包含空格 [英] Check if string contains space
问题描述
我正在尝试检查刺中是否有空格.以下对我不起作用.
I am trying to check if a sting has a space in it. The following is not working for me.
if (skpwords.contains(lcase(query)) And Mid(query, InStrRev(query, " "))) then
end if
推荐答案
检查字符串是否包含字符(或子字符串)的正确方法是使用 InStr()
函数.它将返回字符串中找到文本的位置的基于 1 的索引.因此,返回值 > 0 表示查找成功.例如:
The proper way to check if a string contains a character (or substring) is to use the InStr()
function. It will return the one-based index of the position within the string where the text was found. So, a return value > 0 indicates a successful find. For example:
If InStr(query, " ") > 0 Then
' query contains a space
End If
InStr()
函数也可以选择接受三个或四个参数.如果要指定起始索引,请使用三参数版本:
The InStr()
function can optionally take three or four arguments as well. If you want to specify a starting index, use the three-argument version:
If InStr(3, query, " ") > 0 Then
' query contains a space at or after the 3rd character
End If
如果要执行不区分大小写的搜索(默认为区分大小写),请使用四参数版本.请注意,此函数没有允许您指定区分大小写的三参数版本.如果要执行不区分大小写的搜索,则必须始终提供起始索引,即使要从开头 (1
) 开始搜索:
If you want to perform a case-insensitive search (the default is case-sensitive), then use the four-argument version. Note that there is no three-argument version of this function that allows you to specify case sensitivity. If you want to perform a case-insensitive search, you must always supply the starting index, even if you want to start your search at the beginning (1
):
If InStr(1, query, "A", vbTextCompare) > 0 Then
' query contains "a" or "A"
End If
这篇关于检查字符串是否包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!