我有一个字符串,在结尾附近包含“-”。我想将所有内容都返回到该连字符的左侧。
我不知道如何使用 Split() 或 Regex() 来做到这一点。
处理删除连字符和非连字符大小写的两种方法
Sub Test1()
Dim StrTest As String
StrTest = "I have a hypen-somewhere"
If InStr(StrTest, "-") > 0 Then
MsgBox Left$(StrTest, InStr(StrTest, "-") - 1)
Else
MsgBox "Not found"
End If
End Sub
Sub Test2()
Dim StrTest As String
Dim vString
StrTest = "I have a hypen-somewhere"
vString = Split(StrTest, "-")
If UBound(vString) > 0 Then
MsgBox vString(0)
Else
MsgBox "Not found"
End If
End Sub
在此处Instr()
使用,Mid()
和的组合Len()
您可以尝试以下方法:
Dim hyphenString As String = "hello-world"
Dim leftSide As String = Left(hyphenString, InStr(hyphenString, "-"))
leftSide
现在应该包含“你好”