C# Getting Last Character of String

If we want to get the last character of a string, we can do this using the Substring() and Length methods.

The following example shows how it is done..

namespace ConsoleApplicationTest
{
    class Program
    {
        static void Main(string[] args)
        {

            string myStr = "I love coding";

            string lastChar = myStr.Substring(myStr.Length - 1);

            Console.WriteLine(lastChar);

            Console.ReadLine();

        }
    }
}

It will be printed on the screen as "g".



You May Interest

Finding Character Count of String in C#

C# Example of Sum of Even Numbers from 1 to 100

C# Finding Cosine of an Angle

C# Finding the Path to the Windows System32 Folder

C# Finding Tangent of an Angle