InStr vs .IndexOf

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Let's say I want to find the occurance of SearchString in MyString, but I
want to search without regard to case ...

Which is better and why in VB.Net?

Index = Instr(MyString, SearchString) - 1

or

Index = MyString.ToUpper.IndexOf(SearchString.ToUpper)
 
PJSimon said:
Let's say I want to find the occurance of SearchString in MyString, but I
want to search without regard to case ...

Which is better and why in VB.Net?

Index = Instr(MyString, SearchString) - 1

or

Index = MyString.ToUpper.IndexOf(SearchString.ToUpper)

I prefer 'InStr(..., ..., CompareMethod.Text)'. By specifying the compare
mode, my code is "immune" against changes of 'Option Compare' on project or
file level.
 
What about the overhead added by having to .ToUpper both strings? Or is this
negligible?

InStr is still an excepted method in VB.NET, even tho it's from VB6?
 
PJSimon said:
What about the overhead added by having to .ToUpper both strings? Or is
this
negligible?

This depends on what you are doing. If you are comparing in an algorithm
that performs lots of iterations it may slow down your code, but the user
won't notice anything when you only compare one string to another when
he/she clicks a button, for example.
InStr is still an excepted method in VB.NET, even tho it's from VB6?

'InStr' is a VB.NET method that is neither deprecated nor obsolete.
 
Thanks!

Herfried K. Wagner said:
This depends on what you are doing. If you are comparing in an algorithm
that performs lots of iterations it may slow down your code, but the user
won't notice anything when you only compare one string to another when
he/she clicks a button, for example.


'InStr' is a VB.NET method that is neither deprecated nor obsolete.
 
PJ

I missed this question, which I almost standard answer with that with did a
test about it in this newsgroup in the year 2003.

Instr is faster than indexOf when it is about strings.
Instr is slower than indexOf when it is about characters.

I hope this gives some more information.

Cor
 

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