Left of a character in a string in vb.net

前端 未结 8 1233
耶瑟儿~
耶瑟儿~ 2021-01-03 20:24

say if I have a string 010451-09F2

How to I get left of - from the above string in vb.net

I want 010451

The left fu

8条回答
  •  旧巷少年郎
    2021-01-03 21:16

    Given:

    Dim strOrig = "010451-09F2"
    

    You can do any of the following:

    Dim leftString = strOrig.Substring(0, strOrig.IndexOf("-"))
    

    Or:

    Dim leftString = strOrig.Split("-"c)(0) ' Take the first index in the array
    

    Or:

    Dim leftString = Left(strOrig, InStr(strOrig, "-"))
    ' Could also be: Mid(strOrig, 0, InStr(strOrig, "-"))
    

提交回复
热议问题