你如何在连字符上使用Split()?

M M*_*M M 2 vba

我有一个字符串,在末尾附近包含" - ".我想把那个连字符左边的所有东西都归还.

我不知道如何使用Split()或Regex()来做到这一点.

bre*_*tdj 6

处理删除连字符和非连字符的两种方法

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
Run Code Online (Sandbox Code Playgroud)