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

时间:2012-04-13 04:22:14

标签: vba

我的字符串在末尾附近包含“ - ”。我想把那个连字符左边的所有东西都归还。

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

使用Instr()Mid()Len() here

的组合

答案 2 :(得分:1)

您可以尝试以下方式:

Dim hyphenString As String = "hello-world"
Dim leftSide As String = Left(hyphenString, InStr(hyphenString, "-"))

leftSide现在应该包含“hello”