Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collection/string .Contains vs collection/string.IndexOf

Is there a reason to use .Contains on a string/list instead of .IndexOf? Most code that I would write using .Contains would shortly after need the index of the item and therefore would have to do both statements. But why not both in one?

if ((index = blah.IndexOf(something) >= 0)
    // i know that Contains is true and i also have the index
like image 811
Daniel Avatar asked May 14 '10 01:05

Daniel


1 Answers

You are right that IndexOf is a more general operation than Contains. However, Contains is still very useful because it represents the operation explicitly.

if(blah.IndexOf(something) >=0)
{
}

isn't as obvious an operation as

if(blah.Contains(something))
{
}

so if you need the index, then you should use the IndexOf operation, if you only need to know if the string contains the substring then use Contains.

Use the tool for the job it was created for.

like image 162
luke Avatar answered Sep 30 '22 08:09

luke