Check if string contains space

后端 未结 3 1155
死守一世寂寞
死守一世寂寞 2020-12-31 17:28

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, \"         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 18:23

    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
    

    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
    

    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
    

提交回复
热议问题