ASP.Net C# IndexOf String Function

Updated on 18 May 2012,
Published on 03 Jul 2008

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);

String Functions Examples:

You can see the live samples and examples of String functions from the following links:

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("Index of \"Str\" string with startIndex=15: <b>" + strText.IndexOf("Str", 15) + "</b>");

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

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.

Output:

Learn with the help of live samples of C# IndexOf String Function from the following links:

String IndexOf Function 01

String IndexOf Function 02

Continue to next tutorial: ASP.Net C# LastIndexOf String Function to learn the similar functionality like IndexOf function using LastIndexOf function that returns the index of specified string pattern or character that occurs nearest the right end of string text.

7 Responses to "ASP.Net C# IndexOf String Function"
CongPhuoc
I am using C# to do my website
In the signup page, I need to prevent user type special character like '>,<,+,=,/...'
Is there any way to search it at the same time, because if I use IndexOf, my if clause will very long
Do we have any way to take them in an array?
Thank you for help
Ezineasp.net
Hi CongPhuoc

You can use RegularExpressions in C# to validate the user input. Here is a code snippet for you.

1. include the namespace at the top of code page:

[code:cs]
// add namespace for regular expressions
using System.Text.RegularExpressions;
[/code]


2. Use the following code to apply regular expression validator:

[code:cs]
// create Regex object and initialize
// it by passing string pattern.
// Like in your case <>+= and so on.
// use square braces to wrap up the pattern.
Regex myRegEx = new Regex("[<>+=]");


// this is just a variable to pass the string value
// and validate its value using myRegex object.
string myString = "username<>+=1";

// print the output on web page to get the number of matches found.
Response.Write(myRegEx.Matches(myString).Count + "<br />");

// foreach loop to print the matched values.
foreach (Match match in myRegEx.Matches(myString))
{
Response.Write(match.Value + "<br />");
}


if (myRegEx.Matches(myString).Count > 0)
{
// put your code here to validate the user input.
}
[/code]

Hope this will work for you.
Good Luck
Carl
I have a string that consists of the following:

"fmt_map": "22/2000000/9/0/115,34/0/9/0/115,5/0/7/0/0", "vq": "1", "t": "vjVQa1PpcFNukRZVoJn_mGM0yUBJsT8-fbIg3oV6qxs=", "

I am trying to extract the following after the sequence "t": "
vjVQa1PpcFNukRZVoJn_mGM0yUBJsT8-fbIg3oV6qxs=

The value after T changes each time but is always the same length.

The t variable used to start &t and I used the following code to extract what I needed:
int Tlocation = results.IndexOf(@"&t=");
returnvalue = results.Substring(Tlocation+3, 46);

Can anyone help?

hi i have the same problem
jeevi
hi i have the same problem
gopi n jeevi's lovechild
i have the same problem..inherited it i guess
pavani
hiiiii plz help me... count function in .net. actually i want when we button clicks, the number of counts abtained??...no of btn clicks=no. of cliks will be displayed...plz help me...
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email