Some basic syntax questions

theboyholty

New member
Joined
Apr 12, 2011
Messages
3
Programming Experience
Beginner
Ok only one question, but i'm sure i'll have more soon enough.

I want to do in vb.net what I would have done in SQL with the CHARINDEX command.

That is, I want to interrogate a string and ask, 'Does this string contain a hyphen? If so, whereabouts in the string is it?'

I tried Googling this but as with a lot of things in vb.net, there's always a million different answers given with each response seemingly trying to overcomplicate the issue and out-code the previous one. Hence my confusion.

So, in bascis terms, how would I go about this?

Thanks.
 
Investigate "Contains", and "IndexOf"
 
Check out the methods of the String class here: String Class (System)
One or both of InertiaM's suggestions can be applied.
 
Another option is using InStr(string1, string2, compare method). It will return the location in the string of the first occurence of where string2 is in string1. If there is no occurence, a 0 is returned.
 
Another option is using InStr(string1, string2, compare method). It will return the location in the string of the first occurence of where string2 is in string1. If there is no occurence, a 0 is returned.

Not a good option though. That's a hold-over from VB6 and String.IndexOf would be the preferred option. For those who have years of VB6 behind them to keep using such functions because they are so used to doing is one thing, but for those with no VB6 baggage to start using them is a bad idea.
 
IMHO it's also not a good option because most (if not all) of .NET features have moved to be 0-based ie the first letter of a string is character 0. When learning .NET, always think 0 is at the start. Introducing and using InStr would dramatically complicate matters unnecessarily.
 
Back
Top