Home > DeveloperSection > Articles > IndexOf methods in C#

IndexOf methods in C#


C# String  IndexOf 
Ratings:
0 Comment(s)
 1701  View(s)
Rate this:

IndexOf methods in C#

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

{

    class Program

    {

        static void 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

 

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

{

    class Program

    {

        static void 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

 

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

{

    class Program

    {

        static void Main(string[] args)

        {

            string value1 = "MindStick software pvt. ltd.";

 

            string value2 = "Job Opening to .NET Developer";

 

            int index1 = value1.IndexOfAny(new char[] { 's', 'B' });

 

            Console.WriteLine(value1.Substring(index1));

 

            int index2 = value2.IndexOfAny(new char[] { 'D', 'B' });

 

            Console.WriteLine(value2.Substring(index2));

 

            Console.ReadLine();

        }

    }

}

Output

 

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

{

    class Program

    {

        static void 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

 

2.  public int LastIndexOf(string [] value)

 

namespace LastIndexOf

{

    class Program

    {

        static void 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

 

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

{

    class Program

    {

        static void Main(string[] args)

        {

            const string value = "KamlakarRajBhadur";

 

            int index = value.LastIndexOfAny(new char[] {'r'});

 

            Console.WriteLine(index);

 

            Console.ReadLine();

        }

    }

}

Output


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


Don't want to miss updates? Please click the below button!

Follow MindStick