剪切特殊字符串-VBA

剪切特殊字符串-VBA,vba,ms-access,Vba,Ms Access,我的问题是如何检查字符串开头是否有“text”和“x” 例如: If sText = test.docx Then Function = False ElseIF sText = Test_test.docx Then Function = True End If 如何正确剪切此字符串,以及当u之前的文本不是test时,如果字符串中有多个u,它也可以工作您只需检查字符串是否以test开头_ dim res as boolean, filename as string res = false

我的问题是如何检查字符串开头是否有“text”和“x”

例如:

If sText = test.docx Then Function = False
 ElseIF sText = Test_test.docx Then Function = True
End If

如何正确剪切此字符串,以及当u之前的文本不是test时,如果字符串中有多个u,它也可以工作

您只需检查字符串是否以test开头_

dim res as boolean, filename as string
res = false
filename = ""
' if the len is not Superior to 5 (len of test_), don't check
if len(sText) > 5 then
  ' if the left part begin with test_ 
  if left(lcase(sText), 5) = "test_" then
    res = true
    ' if you want to retrieve the filename without test
    filename = mid(sText, 6) 
  end if
end if
使用Instr(),如下所示:

foo="test"&"_"
bar="test_test.docx"
if Instr(bar, foo)>0 then function = true
else function = false
end if
Instr(bar,foo)显示子字符串foo在字符串栏中的位置。 如果没有这样的子字符串,那么它返回零 如果需要检查任何文本,这不是问题,请使用以下条件:

foo="_"
n=4
bar"test_textt.docx"
m=Instr(bar,foo)
if (m>n)and(len(bar)>m) then function=true
else function=false
end if
此处n-在“”之前的字符数 如果您不知道可能有多少个字符,只需将n设置为0或1即可
如果“”可能是最后一个字符,则删除条件
(len(bar)>m)

您可能需要查看拆分。您可以使用
Instr
。试试这个例子:如果InStr(1,“Test_abcde”,“Test”和“u”,vbTextCompare)=1,那么你也可以使用
类似的
操作符:
如果sText类似于“Test_*”,那么
谢谢你的回答,但是开头也可以是ABC_uu或AD_u,所以解决方案一定是另一个对不起,我误读了,我以为你想检查“Test_u”而不是“any Text”。是否要检查任何文本?或者预设了可能的文本?文本是否可以是数字?像123_uu或23_u你好,先生,谢谢你的回答,但这应该可以与任何短信谢谢!很好!