Home  >  Article  >  Backend Development  >  C# String IndexOf()

C# String IndexOf()

王林
王林Original
2024-09-03 15:16:03259browse

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.

Working of C# String IndexOf() Method

  • Whenever there is a need to find the position or index of the first occurrence of the character or a string in the given instance of a string, we make use of the String IndexOf() method.
  • The instance of the string in which the first occurrence of the character or string to be found will have the indexes starting from zero.
  • If the character or string to be found in the given instance of a string is not present in the given instance of the string, then minus one is returned by the String IndexOf() method.

Examples of C# String IndexOf()

Following are the examples as given below:

Example #1

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:

C# String IndexOf()

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.

Example #2

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:

C# String IndexOf()

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.

Example #3

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:

C# String IndexOf()

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# StartsWith()Next article:C# StartsWith()