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

C# Finding the Path to the Windows Fonts Folder

C# Getting Today's Date

C# Finding the Path to My Documents Folder

C# Getting Current Time

C# Finding the Path to the Windows Folder