Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left of a character in a string in vb.net

Tags:

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

like image 792
acadia Avatar asked Sep 22 '09 16:09

acadia


People also ask

How do I get the string after a specific character in VB net?

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.

What is left function in VB net?

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.

What is left in Visual Basic?

Returns a Variant (String) containing a specified number of characters from the left side of a string.

What does left do in VBA?

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).


2 Answers

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, "-")) 
like image 152
Reed Copsey Avatar answered Sep 20 '22 00:09

Reed Copsey


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.

like image 36
Meta-Knight Avatar answered Sep 21 '22 00:09

Meta-Knight