Check the last character of a string in C#
To check last character of a string in C# you can use built in methodEndsWith()
and pass the char as parameter. This method is case sensitive meaning if you pass uppercase and last char is lower case it will return false.Example
using System;
public class Program
{
public static void Main()
{
string myString = "hello World";
if(myString.EndsWith("d"))
{
Console.WriteLine("String end with d char");
}
if(myString.EndsWith("D"))
{
Console.WriteLine("String end with d char");
}else{
Console.WriteLine("String doe not ends with d char");
}
}
}
Output
String end with d char
String doe not ends with d char
String doe not ends with d char