ASP.Net C# IndexOf String Function

by top54u.com on 02 Jul, 2008
   
   
Spotlight.....

In ASP.Net, C# provides IndexOf string function that is most often used to get the position of string pattern passed to IndexOf function. C# IndexOf function returns the index of the first occurrence of the specified string pattern in the provided string. By default IndexOf function starts searching from the 0th index of the string. In C#, IndexOf string function has some overloads that provide the additional functionality to get the index of the specified character or string pattern from the specified starting index and within the specified count or number of characters. Applying some logic to the C# IndexOf string function you can also get index of specified character or string pattern that occurs after first or second time in the provided string. Following are the commonly used overloads of C# IndexOf string function:

 

  1. public int IndexOf(char value);


  2. public int IndexOf(string value);


  3. public int IndexOf(char value, int startIndex);


  4. public int IndexOf(string value, int startIndex);


  5. public int IndexOf(string value, StringComparison comparisonType);


  6. public int IndexOf(char value, int startIndex, int count);


  7. public int IndexOf(string value, int startIndex, int count);


  8. public int IndexOf(string value, int startIndex, StringComparison comparisonType);

 

Above mentioned overloads of C# IndexOf function accept four types of parameters. First parameter refers to char type or string type pattern you want to get the index of. Second type of parameter is integer type start index of the string from which IndexOf string function will start its pattern matching in the associated string value. Third type of parameter is integer type value for count or number of character to search within this specified length of string starting from the specified start index value.

 

Last parameter type is enumerator of StringComparison. It accepts the following options for comparisonType based on Culture and Case of string letters.

 

  • CurrentCulture = 0,

  • CurrentCultureIgnoreCase = 1,

  • InvariantCulture = 2,

  • InvariantCultureIgnoreCase = 3,

  • Ordinal = 4,

  • OrdinalIgnoreCase = 5

 

In ASP.Net, C# IndexOf string function returns the index of specified string pattern if it exists in the string. It returns -1 if specified string pattern does not exist and returns 0 if string is Empty.

 

Example of ASP.Net C# IndexOf String Function

 

string strText = "C# IndexOf String Function";



Response.Write("Index of '#' char: <b>" + strText.IndexOf('#') + "</b>");



Response.Write("<br /><br />");



Response.Write(
"Index of \"Str\" string with startIndex=15: <b>" + strText.IndexOf("Str", 15) + "</b>");



Response.Write(
"<br /><br />");



Response.Write(
"Index of \"Str\" string with startIndex=3 and count=15: <b>" + strText.IndexOf("Str", 3, 15) + "</b>");



Response.Write(
"<br /><br />");



Response.Write(
"Index of \"str\" lower case string with startIndex=3, count=15 and CurrentCultureIgnoreCase: <b>" + strText.IndexOf("str", 3, 15, StringComparison .CurrentCultureIgnoreCase) + "</b>");

 



 

Output:

 

Index of '#' char: 1

Index of "Str" string with startIndex=15: -1

Index of "Str" string with startIndex=3 and count=15: 11

Index of "str" lower case string with startIndex=3, count=15 and CurrentCultureIgnoreCase: 11

Above examples show the use of C# IndexOf string function using different overloads of IndexOf function. In the last example with StringComparison comparisonType parameter value CurrentCultureIgnoreCase is used to search within the string without case-sensitive approach.

 

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
Loading



Spotlight...
related videos.....
recent posts.....
top54u ezines.....
product spotlight.....