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:
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.
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.
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.
Be the first to rate this post
Tags: asp.net 2.0, c#, c# indexof function, c# string function, asp.net string function, asp.net indexof string function, c# plit, c# indexof, c# replace, c# remove, c# replace, c# code, asp.net examples