To get the last name, try this:
public static class ExtensionMethods
{
public static string GetLastname(this string name)
{
string[] parts = name.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string lastname = parts[parts.Length - 1];
return lastname;
}
}
string lastname = name.GetLastname();
If you've a list of names, and if you want to get a list of last names, then first create the
GetLastname
extension method, and then try this:
List<string> lastnames = names.Select(x => x.GetLastname());
More about extension methods:
Extension Methods (C# Programming Guide)[
^]
Hope this helps.