C# String LastIndexOf Method Example

In this source code example, we will demonstrate the usage of String.LastIndexOf() method in C# with an example.

The C# String.LastIndexOf() method is used to find the index position of the last occurrence of a specified character within a String.

C# String LastIndexOf Method Example

The following example demonstrates the use of the String.LastIndexOf() method:

using System;

public class StringDemo
{
    public static void Main(string[] args)
    {
        string s1 = "sourcecodeexamples";

        int result = s1.LastIndexOf("examples");
        
        Console.WriteLine(result);
        
        int result1 = s1.LastIndexOf('o');
        
        Console.WriteLine(result1);
        
    }

}

Output:

10
7

Related C# String Examples

References

https://docs.microsoft.com/en-us/dotnet/api/system.string.lastindexof?view=net-6.0


Comments