Left of a character in a string in vb.net

前端 未结 8 1190
耶瑟儿~
耶瑟儿~ 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:00

    Sorry not sure on the vb syntax, but the c# is

     string mystring ="010451-09F2";
     string whatIwant = mystring.Split('-')[0];
    
    0 讨论(0)
  • This helped

    Dim str1 as string = me@test.com
    Dim str As String
    
    str = Strings.Left(str1, str1.LastIndexOf("@"))
    
    0 讨论(0)
  • 2021-01-03 21:08

    Get the location of the dash first (or do it inline), and use that value for the left. This is old school VBA, but it'll be something like this:

    Left(YourStringWithTheDash, InStr(YourStringWithTheDash)-1)

    0 讨论(0)
  • 2021-01-03 21:10
    Dim str As String = "010451-09F2"
    Dim leftPart As String = str.Split("-")(0)
    

    Split gives you the left and right parts in a string array. Accessing the first element (index 0) gives you the left part.

    0 讨论(0)
  • 2021-01-03 21:14
    dim s as String = "010451-09F2"
    Console.WriteLine(s.Substring(0, s.IndexOf("-")))
    Console.WriteLine(s.Split("-")(0))
    
    0 讨论(0)
  • 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, "-"))
    
    0 讨论(0)
提交回复
热议问题