Friday 10 June 2011

String.Contain Function in Asp.net

This is the small But useful thing to remember. 

We used to check whether a string object contains a specified string(text) using Index Of method.
Here is the typical code to check the existence of a string in an string object. 


//Sample code
string sName = "Hello Nilay";
if(sName.IndexOf("Nilay") > -1)
{
     //Nilay exists in string object sName

Now let's take a closer look at the above code...... What we want is to know whether an instance of string contains some string(text). 

Did I say contains. that's exactly C# .Net 2.0 provides us with. 

Let's revisit the same functionality using 2.0 

//Sample code 
string sName = "Hello Nilay";

if(sName.Contains("Nilay"))
{
     //Nilay exists in string object sName 
}

1 comment: