String Lastindexof()

The C# LastIndexOf function is employed to determine the index of the final instance of a specified character within a String.

Signature

Example

public int LastIndexOf(Char ch)
public int LastIndexOf(Char, Int32)
public int LastIndexOf(Char, Int32, Int32)
public int LastIndexOf(String)
public int LastIndexOf(String, Int32)
public int LastIndexOf(String, Int32, Int32)
public int LastIndexOf(String, Int32, Int32, StringComparison)
public int LastIndexOf(String, Int32, StringComparison)
public int LastIndexOf(String, StringComparison)

Parameter

The ch parameter represents a character type and is utilized to locate the last instance of a specified character within a string.

Return

It returns integer value.

C# String LastIndexOf Method Example

Example

using System; 
    public class StringExample  
    {  
        public static void Main(string[] args)  
        {  
           string s1 = "Hello C#";
           int index = s1.LastIndexOf('l');
           Console.WriteLine(index);
      }  
    }

Output:

C# String IndexOf vs LastIndexOf Example

The IndexOf function provides the position of the initial matching character, while the LastIndexOf function gives the position of the final matching character in a string.

Example

using System; 
    public class StringExample  
    {  
        public static void Main(string[] args)  
        {  
           string s1 = "Hello C#";
           int first = s1.IndexOf('l');
           int last = s1.LastIndexOf('l');
           Console.WriteLine(first);
           Console.WriteLine(last);
      }  
    }

Output:

Input Required

This code uses input(). Please provide values below: