How to separate full name string into firstname and lastname string?

Viewed 123902

I need some help with this, I have a fullname string and what I need to do is separate and use this fullname string as firstname and lastname separately.

18 Answers

name ="Tony Stark is dead";

get_first_name(String name) {
    var names = name.split(' ');
    String first_name= "";
 
    for (int i = 0; i != names.length; i++) {
      if (i != names.length - 1) 
      {
        if (i == 0) {
          first_name= first_name+ names[i];
        } else {
          first_name= first_name+ " " + names[i];
        }
      }
    }
    return first_name; // Tony Stark is
  }



  get_last_name(String name) {
    var names = name.split(' ');
    return names[names.length - 1].toString(); // dead
  }

Easy, simple code to transform something like Flowers, Love to Love Flowers (this works with very complex names such as Williams Jones, Rupert John):

        string fullname = "Flowers, Love";
        string[] fullnameArray = fullname.Split(",");//Split specify a separator character, so it's removed
        for (int i = fullnameArray.Length - 1; i >= fullnameArray.Length - 2; i--)
        {
                Write($"{fullnameArray[i].TrimStart() + " "}");
        } 

output: Love Flowers

The other way around. Love Flowers converted to Flowers, Love:

        string fullname = "Love Flowers";
        int indexOfTheSpace = fullname.IndexOf(' ');
        string firstname = fullname.Substring(0, indexOfTheSpace);
        string lastname = fullname.Substring(indexOfTheSpace + 1);
        WriteLine($"{lastname}, {firstname}");

There is more than one method for this. My specific situation is solved with a code example like below.

for Example


if there is only one space in the user's name.

 int idx = fullName.IndexOf(' '); //This customer name : Osman Veli

if there is more than one space in the user's name.

 if (fullName.Count(Char.IsWhiteSpace) > 1)
 {
    idx = fullName.LastIndexOf(' '); //This customer name : Osman Veli Sağlam
 }

  if (idx != -1)
  {
    // Osman Veli Sağlam
    firstName = fullName.Substring(0, idx); // FirstName: Osman Veli 
    lastName = fullName.Substring(idx + 1); // LastName : Sağlam
   }

you can create a value object to represent a name and use it in your application

public class Name
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    public string FullName { get; }

    public Name(string name)
    {
        if (string.IsNullOrEmpty(name))
            return;

        FullName = name;

        LoadFirstAndLastName();
    }

    private void LoadFirstAndLastName()
    {
        var names = FullName.Trim().Split(" ", 2);

        FirstName = names.First();

        if (names.Count() > 1)
            LastName = names.Last();
    }

    public override string ToString() => FullName;

    //Enables implicit set (Name name = "Rafael Silva")
    public static implicit operator Name(string name) => new Name(name);
}

Usages:

Name name = "Jhon Doe"; //FirstName = Jhon - LastName = Doe
Name name = new Name("Jhon Doe"); //FirstName = Jhon - LastName = Doe    
Name name = new Name("Rafael Cristiano da Silva"); //FirstName = Rafael - LastName = Cristiano da Silva

//On class property
public Name Name {get; set; } = "Jhon Doe";

Better to be late than never, here is my solution for this used in many applications.

 string fullName = "John De Silva";
 string[] names = fullName.Split(" ");
 string firstName = names.First(); // John
 string lastName = string.Join(" ", names.Skip(1).ToArray()); // De Silva

Enjoy !

Sub SplitFullName(Full_Name As String)

        Dim names = Full_Name.Split(" ")
        Dim FirstName As String = ""
        Dim MiddletName As String = ""
        Dim LastName As String = ""
        If names.Count = 0 Then
            FirstName = ""
            MiddletName = ""
            LastName = ""
        ElseIf names.Count = 1 Then
            FirstName = names(0)
        ElseIf names.Count = 2 Then
            FirstName = names(0)
            LastName = names(1)
        Else
            FirstName = names(0)
            For i = 1 To names.Count - 2
                MiddletName += " " & names(i)
            Next
            LastName = names(names.Count - 1)
        End If
        MsgBox("The first name is: " & FirstName & ";   The middle name is: " & MiddletName & ";    The last name is: " & LastName)

End Sub
Related