我的字符串在末尾附近包含“ - ”。我想把那个连字符左边的所有东西都归还。
我不知道如何使用Split()或Regex()来做到这一点。
答案 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”