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

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

lets' say I have a value ABCD - 123

How can I return the position of the dash ?
 
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().
 
Rob said:
lets' say I have a value ABCD - 123

How can I return the position of the dash ?

'Strings.InStr', 'String.IndexOf'.
 
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.
 
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, "-"))
 
AMercer said:
Do you mean that you have a string containing "ABCD - 123"? If so, use
Instr().

The "proper" .NET way would be to use the IndexOf method.
 
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

Back
Top