Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

separate first middle last name C#

Tags:

c#

parsing

names

Goal: parse name when user enters name, and have a message box display with first middle and last name. Right now it only works when you type in three names, if you try two it crashes, and I'm sure it's cause of my array but Im not sure where I'm wrong. Super novice, learning on my own so any help would be greatly appreciated!!

P.S. GUI the user sees is just an entry block for them to enter their name into one line, spacing between each word.

 private void btnParseName_Click(object sender, System.EventArgs e)
    {
        string fullName = txtFullName.Text;
        fullName = fullName.Trim();

        string[] names = fullName.Split(' ');

        string firstName = "";
        string firstLetter = "";
        string otherFirstLetters = "";
        if (names[0].Length > 0)
        {
            firstName = names[0];
            firstLetter = firstName.Substring(0, 1).ToUpper();
            otherFirstLetters = firstName.Substring(1).ToLower();
        }

        string secondName = "";
        string secondFirstLetter = "";
        string secondOtherLetters = "";
        if (names[1].Length > 0)
         {
            secondName = names[1];
            secondFirstLetter = secondName.Substring(0, 1).ToUpper();
            secondOtherLetters = secondName.Substring(0).ToLower();
         }

        string thirdName = "";
        string thirdFirstLetter = "";
        string thirdOtherLetters = "";
        if (names[2].Length > 0)
        {

            thirdName = names[2];
            thirdFirstLetter = thirdName.Substring(0, 1).ToUpper();
            thirdOtherLetters = thirdName.Substring(0).ToLower();

        }

        MessageBox.Show(
                "First Name:         " + firstLetter + otherFirstLetters + "\n\n" +
                "Middle Name:        " + secondFirstLetter + secondOtherLetters + "\n\n" +
                "Last Name:          " + thirdFirstLetter + thirdOtherLetters);
like image 844
Austin D Avatar asked Jul 29 '17 05:07

Austin D


1 Answers

Here is the working example how you can do it:

public class FullName
{
    public  string FirstName { get; set; }
    public  string MiddleName { get; set; }
    public  string LastName { get; set; }

    public FullName()
    {

    }

    public FullName(string fullName)
    {
        var nameParts = fullName.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries);

        if (nameParts == null)
        {
            return;
        }
        if (nameParts.Length > 0)
        {
            FirstName = nameParts[0];
        }
        if (nameParts.Length > 1)
        {
            MiddleName = nameParts[1];
        }
        if (nameParts.Length > 2)
        {
            LastName = nameParts[2];
        }
    }

    public override string ToString()
    {
        return $"{FirstName} {MiddleName} {LastName}".TrimEnd();
    }
}

Usage example:

class Program
{
    static void Main(string[] args)
    {
        var fullName = new FullName("first middle last");
        Console.WriteLine(fullName);
        Console.ReadLine();
    }
}
like image 78
Vano Maisuradze Avatar answered Oct 27 '22 23:10

Vano Maisuradze