Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone know what's the difference between IndexOf and InStr?
And can u give me an example how to use it?
Posted

Have a look at MSDN site: InStr[^], IndexOf[^]
 
Share this answer
 
Comments
VJ Reddy 30-May-12 8:45am    
Good references. 5!
You are fast :)
Maciej Los 30-May-12 8:53am    
Thank you, VJ ;)
Manfred Rudolf Bihy 30-May-12 8:57am    
Sweet and short! 5+
Maciej Los 30-May-12 9:05am    
Thank you ;)
Mohamed Mitwalli 30-May-12 9:10am    
5+
Being able to google and reading what you find surely helps in this case:

String.IndexOf[^]

InStr()[^]

So the former is an instance method of the class string and the latter is a Visual Basic runtime member (aka built in) function where the arguments may be nothing. IndexOf always needs an string instance to operate on.

Regards,

Manfred
 
Share this answer
 
v2
Comments
VJ Reddy 30-May-12 8:46am    
Good points and speed. 5!
Manfred Rudolf Bihy 30-May-12 8:56am    
Thanks! :)
Maciej Los 30-May-12 9:05am    
Good answer, my 5!
Manfred Rudolf Bihy 30-May-12 9:08am    
Thanks! :)
Mohamed Mitwalli 30-May-12 9:10am    
5+
As can be seen from here http://msdn.microsoft.com/en-us/library/8460tsh1(v=vs.90).aspx[^], InStr is a VisualBasic specific Shared function, which returns the first occurance of one string in another string. The start index is 1 based. It returns 0 in case of non finding the string.

As can be seen from here http://msdn.microsoft.com/en-us/library/k8b1470s.aspx[^] the, IndexOf is an instance method of string class of .NET framework and can be used in VB and C#. The start index is 0 based and returns -1 in case of non finding the string.

The examples for usage of the above functions are given at the above references.

I think the IndexOf method is good to use as it is common for all .NET languages.
 
Share this answer
 
v2
Comments
Maciej Los 30-May-12 8:52am    
Good answer. +5!
VJ Reddy 30-May-12 8:57am    
Thank you, losmac :)
Manfred Rudolf Bihy 30-May-12 9:00am    
Good one! 5+

A little more verbose than mine ;).
VJ Reddy 30-May-12 9:04am    
Thank you, Manfred :)
Mohamed Mitwalli 30-May-12 9:11am    
5+
The most important difference is this: System.String.IndexOf is a standard CLR method, a function of a standard part of .NET, Mono or any other standard CLR implementations and hence, fully compatible with any of them regardless of the language used; standardized under ECMA and ISO standard organizations. And InStr is no more than proprietary legacy never-standardized VB garbage. Think by yourself what to use.

—SA
 
Share this answer
 
Comments
VJ Reddy 30-May-12 13:21pm    
Agreed. It is an important difference. 5!
Sergey Alexandrovich Kryukov 30-May-12 13:48pm    
Thank you, VJ.
--SA
Maciej Los 30-May-12 13:47pm    
Good point, my 5!
Sergey Alexandrovich Kryukov 30-May-12 13:48pm    
Thank you, Maciej.
--SA
Maciej Los 31-May-12 1:44am    
You're welcome, Sergey ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900