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

  • Thread starter Thread starter Rob
  • Start date Start date
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, "-"))
 
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
 
Back
Top