ExtensionMethod.NET Home of 881 C#, Visual Basic, F# and Javascript extension methods

LastChar

Select Last character in string .

Source

public static class MehrdadExtensions
{
 public static string LastChar(this string input)
 {
 if (!string.IsNullOrEmpty(input))
 {
 if (input.Length >= 1)
 {
 return input.Substring(input.Length - 1, 1);
 }
 else
 {
 return input;
 }
 }
 else
 {
 return null;
 }
 }
}

Example

string name = "mehrdad";
Response.Write("Name is : " + name);
Response.Write("<br />");
Response.Write("Last Char : "+name.LastChar());

Author: Mehrdad Ghasemi

Submitted on: 28 mrt. 2009

Language: C#

Type: System.String

Views: 5902