削减特殊字符串 - VBA

时间:2017-02-14 10:18:50

标签: vba access

我的问题是如何检查字符串是否有"文本" &安培; " _"一开始。

例如:

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

我是如何正确剪切这个字符串的,当_之前的文字没有测试时,如果字符串中有几个_它也可以正常工作

2 个答案:

答案 0 :(得分:0)

使用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即可 如果“”migth是最后一个字符,则删除条件(len(bar)>m)

答案 1 :(得分:-1)

您只需检查字符串是否以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