articles

Home / DeveloperSection / Articles / IndexOf methods in C#

IndexOf methods in C#

Anonymous User7117 13-Nov-2014

In this article I’m explaining about IndexOf () method of string class in C#.

The IndexOf method can be used to check if one character is inside of another. For example, suppose you want to check an email address to see if it contains the @ character. If it doesn't you can tell the user that it's an invalid email address.


1-The IndexOf Method: It reports a zero-based index the first time it encounters the Unicode character you specified in the string in the program. It will report a -1 value if it doesn’t encounter the character.

2-The IndexOfAny Method: It reports a zero-based index the first time it encoutners the Unicode character you specified in an array of Unicode characters in the program. It will report a -1 value if it doesn’t encounter the character in the program.

3-The LastIndexOf Method: It reports the zero-based index of the Unicode character you specify the final time it encounters the character in the string. This means that if there are two character occurrences in the string, it will report the index of the character it encountered last. It will report a -1 value if it doesn’t encounter the character in the program.

4-TheLastIndexOfAny Method: It reports the zero-based index of one or more specified Unicode characters the final time they occur in the Unicode array in the program. It will report a -1 value if it doesn’t encounter the specified characters in the program. 

IndexOf Method is overloaded

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)
9.     public int IndexOf(string value, int startIndex, int count, StringComparison
comparisontype)
Example:
1.  public int IndexOf(char value)

In this example we use IndexOf simply to see whether the input string contains a character. We want to see if the string in the example contains ‘@’ .

namespace IndexOf1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            string email = "kamlakarsingh4@gmail.com";
 
            int result = email.IndexOf('@');
 
            if (result == -1)
            {
                Console.WriteLine("Invalid Email Address!!");
            }
            else
            {
                Console.WriteLine("Index No of @  =  " + result);
            }
            Console.ReadLine();
        }
    }
}

 

Output

 IndexOf methods in C#

2.  public int IndexOf(string value)

In this example we use IndexOf simply to see whether the input string contains a string. We want to see if the string in the example contains “Good” .

namespace IndexOf1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            string text = "MindStick Software Pvt. ltd. is Good Development company";
            int result = text.IndexOf("Good");
            if (result == -1)
            {
                Console.WriteLine("Invalid text!!");
            }
            else
            {
                Console.WriteLine("MindStick Software Pvt. ltd. is Good Development company");
            }
            Console.ReadLine();
        }
    }
}
 
Output

 IndexOf methods in C#

IndexOfAny Method is overloaded

1.     public int IndexOfAny(char [] value)

2.     public int IndexOfAny(char [] value, int startIndex)

3.     public int IndexOfAny(char [] value, int startIndex, int count)

Example:

1.  public int IndexOfAny(char [] value)

namespace IndexOf2
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            string value1 = "MindStick software pvt. ltd.";
 
            string value2 = "Job Opening to .NET Developer";
 
            int index1 = value1.IndexOfAny(newchar[] { 's', 'B' });
 
            Console.WriteLine(value1.Substring(index1));
 
            int index2 = value2.IndexOfAny(newchar[] { 'D', 'B' });
 
            Console.WriteLine(value2.Substring(index2));
 
            Console.ReadLine();
        }
    }
}
Output

 IndexOf methods in C#

LastIndexOf Method is overloaded

1.     public int LastIndexOf(char [] value)

2.     public int LastIndexOf(string [] value)

3.     public int LastIndexOf(char [] value, int startIndex)

4.     public int LastIndexOf(string [] value, int startIndex)

5.     public int LastIndexOf(string [] value, StringComparison comparisontype)

6.     public int LastIndexOf(char [] value, int startIndex,  int count)

7.     public int LastIndexOf(string [] value, int startIndex,  int count)

8.     public int LastIndexOf(string [] value, int startIndex, StringComparison

comparisontype)

9.     public int LastIndexOf(string [] value, int startIndex, int count,

StringComparison comparisontype)

Example

1.  public int LastIndexOf(char [] value)

searches strings in reverse. It finds the location of the last occurrence of a letter or substring in a C# string. It declaratively locates the last position without a for-loop.

namespace LastIndexOf
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            string value = "Dot Net Perls";
 
            int index1 = value.LastIndexOf('N');
 
            if (index1 != -1)
            {
                Console.WriteLine("Index NO = "+index1);
                Console.WriteLine(value.Substring(index1));
            }
            Console.ReadLine();
        }
    }
}
 
Output

 IndexOf methods in C#

2.  public int LastIndexOf(string [] value)

 

namespace LastIndexOf
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            string value = "Dot Net Perls";
 
            int index2 = value.LastIndexOf("Perls");
            if (index2 != -1)
            {
                Console.WriteLine(index2);
                Console.WriteLine(value.Substring(index2));
            }
            Console.ReadLine();
        }
    }
}
Output

 IndexOf methods in C#

LastIndexOfAny Method is overloaded 
1.     public int LastIndexOfAny(char [] value)
2.     public int LastIndexOfAny(char [] value, int startIndex)
3.     public int LastIndexOfAny(char [] value, int startIndex, int count)

 

Example

1.  public int LastIndexOfAny(char [] value)

namespace LastIndexOfAny
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            conststring value = "KamlakarRajBhadur";
 
            int index = value.LastIndexOfAny(newchar[] {'r'});
 
            Console.WriteLine(index);
 
            Console.ReadLine();
        }
    }
}
Output

IndexOf methods in C#


in my next post i'll discuss about split methods in c#


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By