The string method which is used to find the first occurrence of a given character or a string from the given instance of the string in which the indexes begin from zero is called String Indexof() method in C# and this method returns minus one if the character or string to be found is not present in the given instance of the string and the index of the character or string to be found, is an integer returned by using this method.
Syntax:
The syntax of the C# String IndexOf() method is as follows:
public intIndexOf (string string_name);
Where string_name is the character or string to be found in the given instance of the string. Since the index of the character or string of the given instance of the string returned by this method, the type is int.
Following are the examples as given below:
C# program to demonstrate String IndexOf() method to find the first occurrence of the character or string from the given instance of the string:
Code:
using System; //a class called check is called class check { //main method is called static void Main() { //a string variable is used to store the string from which the index of the letter e for all the occurrences must be found and the substring following the letter e must be printed string str = "Welcome to C#"; //We are looping through all instances of the letter e in the given string int j = 0; while ((j = str.IndexOf('e', j)) != -1) { // we are using substring method to find out the substring starting from each occurrence of the letter e Console.WriteLine(str.Substring(j)); // the index is incremented until the indexof method returns -1 and the loop ends j++; } } }
Output:
In the above program, a class called check is called. Then the main method is called within which a string variable is defined to store the string from which the index of the letter e for all the occurrences must be found and the substring following the letter e must be printed. In the expression str.IndexOf(e, j) in the above program, j indicates the index position from which the occurrence of the letter e must be searched and it is incremented as long as there is no more occurrence of the letter e in the given string and str.IndexOf(e,j) expression returns a -1. substring(j) is used to obtain the sub string.
C# program to demonstrate string IndexOf method to find the occurrence of a string in the given string and then print the substring of the given string starting from the index position specified as the position of the given character:
Code:
using System; //a class called check is defined class check { //main method is called static void Main() { // a string variable is used to store the string from which the specified string must be found const string val = "Welcome to C#"; //Using IndexOf method to find the occurrence of the given string in the specified string if (val.IndexOf("C#") != -1) { Console.WriteLine("The string C# is present in the specified string"); } //IndexOf method is used again to find the index of the first occurrence of the letter C and substring method is used to print the substring followed by the first occurrence of the letter C int j = val.IndexOf("C"); Console.WriteLine(val.Substring(j)); } }
Output:
In the above program, a namespace called check is created. Then the main method is called within which a string variable is defined to store the string from which the first occurrence of the specified string is to be found. Then the IndexOf method is used to find the occurrence of the given string in the specified string. Then the IndexOf method is used again to find the index of the first occurrence of the letter C and the substring method is used to print the substring followed by the first occurrence of the letter C.
C# program to demonstrate String IndexOf() method to find the first occurrence of the character or string from the given instance of the string:
Code:
using System; //a class called check is defined class check { //main method is called static void Main() { // a string variable is used to store the string from which the specified string must be found const string val = "12,34"; //Using IndexOf method to find the occurrence of the given string in the specified string if (val.IndexOf(",") != -1) { Console.WriteLine("The character , is present in the specified string"); } } }
Output:
In the above program, a class called check is called. Then the main method is called within which a string variable is used to store the string from which the specified string must be found. Then the IndexOf method is used to find the occurrence of the given string in the specified string.
The above is the detailed content of C# String IndexOf(). For more information, please follow other related articles on the PHP Chinese website!