tags:

views:

323

answers:

7

Hello,

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

A: 

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)

Sarkazein
+3  A: 
Dim str As String = "010451-09F2"
Dim leftPart As String = str.Split("-"c)(0)

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

Meta-Knight
+1 but please get rid of that c error....
JL
What is a "c error"?
AMissico
You mean get rid of the Char data type declaration by removing the c.
AMissico
It's not an error. It's used to indicate that "-" is a character and not a string. (equivalent to '-' in C#)
Meta-Knight
@JL, it is not an error because Split allows "ByVal ParamArray separator() As Char".
AMissico
@AMissico: JL is a C# programmer that doesn't know VB's syntax. I didn't know taht you defined a char that way in that language and too thought it was a typo.
voyager
+1. Also works if the string doesn't contain a "-"
MarkJ
+1  A: 

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

 string mystring ="010451-09F2";
 string whatIwant = mystring.Split('-')[0];
JL
A: 
dim s as String = "010451-09F2"
Console.WriteLine(s.Substring(0, s.IndexOf("-")))
Console.WriteLine(s.Split("-")(0))
shahkalpesh
+4  A: 

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, "-"))
Reed Copsey
Add Mid/Left with InStr, remove the ugly Split, and this answer is complete.
AMissico
It's worth mentioning that if the string doesn't contain a "-" the Substring...IndexOf will throw an ArgumentOutOfRange exception, but the Split method just returns the whole string.
MarkJ
@AMissico: Added them for completeness ;)
Reed Copsey
@MarkJ - Okay, I accept the non-attractive Split. :O)
AMissico
Dear Readers: Be aware that Left will conflict with Form's Left property, so you may need to qualify it.
AMissico
@AMissico: Yes - that's potentially true, IF this is being used in a Windows Forms project, inside of a Form's code. If not, then it's not an issue.
Reed Copsey
A: 

Use something like this:

Mid("010451-09F2",1,InStr("-"))

Neil
This VB6 code. In VB.net you have more advanced structures that aer more readable.
voyager
It is not VB6 code. It is Visual Basic statements and it is much more readable to me then the "advanced" stuff. In general, these Visual Basic statements are more complete and robust then the equivalent .NET Framework methods.
AMissico
I dont have much idea about string fundtions in VB .Net. I have used it many times but thought something like this you can find in your vb .net. Thank for your information on this.I have used this in C#:string s ="010451-09F2"; s= s.Substring(1, s.IndexOf("-")-1); and it worked
Neil
A: 
    Dim sValue As String = "010451-09F2"
    Debug.WriteLine(sValue.Substring(0, sValue.IndexOf("-"c)))
AMissico