Home All Groups Group Topic Archive Search About

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

Author
28 Sep 2006 12:20 PM
Rob
lets' say I have a value ABCD - 123

How can I return the position of the dash ?

Author
28 Sep 2006 12:56 PM
AMercer
> 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().
Author
28 Sep 2006 5:51 PM
zacks
AMercer wrote:
> > 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().

The "proper" .NET way would be to use the IndexOf method.
Author
29 Sep 2006 8:46 AM
Andrew Morton
za***@construction-imaging.com wrote:
> AMercer wrote:
>>> 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().
>
> 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
Author
28 Sep 2006 1:27 PM
Nate
x = instr("ABCD - 123", "-")

Show quoteHide quote
"Rob" <rwch***@comcast.net> wrote in message
news:cOCdnXEKArCAIIbYnZ2dnUVZ_rKdnZ2d@comcast.com...
> lets' say I have a value ABCD - 123
>
> How can I return the position of the dash ?
>
Author
28 Sep 2006 1:28 PM
Kevin S Gallagher
Dim s As String = "ABCD - 123"
s.IndexOf("-").ToString()

Show quoteHide quote
"Rob" <rwch***@comcast.net> wrote in message
news:cOCdnXEKArCAIIbYnZ2dnUVZ_rKdnZ2d@comcast.com...
> lets' say I have a value ABCD - 123
>
> How can I return the position of the dash ?
>
Author
28 Sep 2006 1:43 PM
Herfried K. Wagner [MVP]
"Rob" <rwch***@comcast.net> schrieb:
> lets' say I have a value ABCD - 123
>
> How can I return the position of the dash ?

'Strings.InStr', 'String.IndexOf'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
28 Sep 2006 4:26 PM
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.

Rob wrote:
Show quoteHide quote
> lets' say I have a value ABCD - 123
>
> How can I return the position of the dash ?
Author
28 Sep 2006 4:29 PM
Tom Shelton
Rob wrote:
> 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, "-"))

--
Tom Shelton