Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 检查一个字符串是否包含另一个字符串_String_Vba - Fatal编程技术网

String 检查一个字符串是否包含另一个字符串

String 检查一个字符串是否包含另一个字符串,string,vba,String,Vba,我想知道字符串中是否包含“,”(逗号)。除了逐字符读取之外,我们还有其他选择吗?使用该函数 将在pos中返回15 如果未找到,将返回0 如果需要使用excel公式查找逗号,可以使用=find(“,”A1)函数 请注意,如果要使用Instr查找字符串的位置(不区分大小写),请使用Instr的第三个参数,并为其指定constvbTextCompare(对于顽固分子,仅为1) 将为您提供14的值 请注意,在这种情况下,您必须按照我链接的规范中的说明指定开始位置:如果指定了compare,则需要star

我想知道字符串中是否包含“,”(逗号)。除了逐字符读取之外,我们还有其他选择吗?

使用该函数

将在pos中返回15

如果未找到,将返回0

如果需要使用excel公式查找逗号,可以使用
=find(“,”A1)
函数

请注意,如果要使用
Instr
查找字符串的位置(不区分大小写),请使用Instr的第三个参数,并为其指定const
vbTextCompare
(对于顽固分子,仅为1)

将为您提供14的值

请注意,在这种情况下,您必须按照我链接的规范中的说明指定开始位置:如果指定了compare,则需要start参数。

还有一个函数,它执行相同类型的操作,但从文本末尾开始搜索

根据@rene的回答

Dim pos As Integer
pos = InStrRev("find the comma, in the string", ",")
…仍将返回15到pos,但如果字符串包含多个搜索字符串,如单词“the”,则:


…将返回20到pos,而不是6。

基于Rene的答案,您还可以编写一个函数,如果子字符串存在,则返回TRUE;如果子字符串不存在,则返回FALSE:

Public Function Contains(strBaseString As String, strSearchTerm As String) As Boolean
'Purpose: Returns TRUE if one string exists within another
On Error GoTo ErrorMessage
    Contains = InStr(strBaseString, strSearchTerm)
Exit Function
ErrorMessage:
MsgBox "The database has generated an error. Please contact the database administrator, quoting the following error message: '" & Err.Description & "'", vbCritical, "Database Error"
End
End Function

您还可以使用特殊单词
,如

Public Sub Search()
  If "My Big String with, in the middle" Like "*,*" Then
    Debug.Print ("Found ','")
  End If
End Sub

考虑到现有的Instr/InstrRev函数,您不会真的想这样做,但有时使用EVALUATE返回VBA中Excel工作表函数的结果很方便

Option Explicit

Public Sub test()

    Debug.Print ContainsSubString("bc", "abc,d")

End Sub
Public Function ContainsSubString(ByVal substring As String, ByVal testString As String) As Boolean
    'substring = string to test for; testString = string to search
    ContainsSubString = Evaluate("=ISNUMBER(FIND(" & Chr$(34) & substring & Chr$(34) & ", " & Chr$(34) & testString & Chr$(34) & "))")

End Function

INSTR
是否适用于您?我们希望此函数中出现哪种类型的数据库错误?错误捕获和错误消息似乎完全没有意义。@RoobieNuby这只是我默认的错误处理。我把它放在我所有的函数中,因为如果出现问题,我希望工作人员给我打电话,而不是自己尝试修复它。链接到模式格式,但是如果找到的字符串位于位置0怎么办?如何区分“在索引0上找到”和“未找到(0)”?@gEdringer。当要查找的字符串位于开头时,它返回1。
Public Function Contains(strBaseString As String, strSearchTerm As String) As Boolean
'Purpose: Returns TRUE if one string exists within another
On Error GoTo ErrorMessage
    Contains = InStr(strBaseString, strSearchTerm)
Exit Function
ErrorMessage:
MsgBox "The database has generated an error. Please contact the database administrator, quoting the following error message: '" & Err.Description & "'", vbCritical, "Database Error"
End
End Function
Public Sub Search()
  If "My Big String with, in the middle" Like "*,*" Then
    Debug.Print ("Found ','")
  End If
End Sub
Option Explicit

Public Sub test()

    Debug.Print ContainsSubString("bc", "abc,d")

End Sub
Public Function ContainsSubString(ByVal substring As String, ByVal testString As String) As Boolean
    'substring = string to test for; testString = string to search
    ContainsSubString = Evaluate("=ISNUMBER(FIND(" & Chr$(34) & substring & Chr$(34) & ", " & Chr$(34) & testString & Chr$(34) & "))")

End Function