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 function doesn't allow me to specify seperator character.
Thanks
The substring function is used to obtain a part of a specified string. This method is defined in the String class of Microsoft VB.NET. You have to specify the start index from which the String will be extracted. The String will be extracted from that index up to the length that you specify.
The Left function returns a specified number of characters from the left side of a string. Tip: Use the Len function to find the number of characters in a string.
Returns a Variant (String) containing a specified number of characters from the left side of a string.
The VBA LEFT function is listed under the text category of VBA functions. When you use it in a VBA code, it returns a sub-string from a string from the starting position. In simple words, you can extract a number of characters from a string from its starting (which is the left side).
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, "-"))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With