How to find the position of the first dash in a string

G

Guest

lets' say I have a value ABCD - 123
How can I return the position of the dash ?

Do you mean that you have a string containing "ABCD - 123"? If so, use
Instr().
 
S

Shane

dim i as integer = YourString.IndexOf("-")
if i < 0 the pattern does not exist in the string, otherwise, "i" has
the offset of the start of the string.
 
T

Tom Shelton

Rob said:
lets' say I have a value ABCD - 123

How can I return the position of the dash ?

Dim s As String = "ABCD - 123"
Console.WriteLine (s.IndexOf ("-"))

or

Dim s As String = "ABCD - 123"
Console.WriteLine (InStr (s, "-"))
 
A

Andrew Morton

The "proper" .NET way would be to use the IndexOf method.

But surely, then you should check that the string isn't Nothing first or you
could get an ArgumentNullException . Why do you consider the VB.NET method
as not "proper" when programming in VB.NET? Would you exclusively use
"String.Concat" rather than "&" ?.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top