GithubHelp home page GithubHelp logo

fullname_splitter's Introduction

Name Splitter in C#

This small program splits full name into first name, middle namd and last name. This program consider that the fullName may have either comma (,) only or spaces only. If comma appears, it splits by comma, it assumes that the first part is last name and 2nd part is first name and middle name. If only spaces appears, it splits by the number of words.

Return First Name, Last Name and Middle Name in array

Returns in array with 3 items. First one is first name, middle one is middle name and last one is last name.

    static void Main(string[] args)
    {
        string strError = "";
        string[] names;
        //string[] names = GetSplitNames("Aauvince Marc,Marie Kane B", ref strError);
        // string[] names = GetSplitNames("Mountbatten-Windsor, Archie Harrison", ref strError);
        // string[] names = GetSplitNames("Jonshon, Marry Ann Sue Butler", ref strError);
        //string[] names = GetSplitNames("Marry Ann Sue Butler", ref strError);
        string strFullName;
        do
        {
            Console.WriteLine("Enter Full Name (Type exit to close program): ");
            strFullName = Console.ReadLine();
            names = GetSplitNames(strFullName, ref strError);
            Console.WriteLine("First Name:" + names[0]);
            Console.WriteLine("Middle Name:" + names[1]);
            Console.WriteLine("Last Name:" + names[2]);
            Console.WriteLine("Error if any: " + strError);

        } while (strFullName != "exit");

       // string[] names = GetSplitNames("Williams,Valerie L", ref strError);
        

       //Console.WriteLine("First Name:" + names[0]);
       // Console.WriteLine("Middle Name:" +names[1]);
       // Console.WriteLine("Last Name:" + names[2]);
       // Console.WriteLine("Error if any: " + strError);
       // Console.ReadLine();
    }
    
    public static string[] GetSplitNames(string strFullName, ref string strError)
    {
        //Returns as FirstName, MiddleName, LastName
        //strErrorCode: NO-NAME - empty name, ONE-WORD - Only one word, MORE-THAN-FOUR - More than 4 words, Else Error message
        if (strFullName.Trim() == "")
        {
            strFullName = "NO-NAME";
            return null;
        }
        string[] strNames = new string[3];
        //If Comman is there - LastName, FirstName
        Regex regularEx = new Regex(@",");
        if (regularEx.IsMatch(strFullName.Trim()))
        {
            string[] strTemp = strFullName.Trim().Split(',');
            //First part before comma , is the Last Name
            strNames[2] = strTemp[0].Trim();   //Last Name
            //Split 2nd part for fist name and last name
            string[] strTemp2 = strTemp[1].Trim().Split(' ');  //Split by space
            switch (strTemp2.Length)
            {
                case (0):
                    strNames[0] = "";   //first Name
                    strNames[1] = "";   //Middle Name
                    strError = "NO-FIRST-NAME";
                    break;
                case (1):
                    strNames[0] = strTemp2[0].Trim();
                    strNames[1] = "";
                    strError = "";
                    break;
                case (2):
                    strNames[0] = strTemp2[0].Trim();
                    strNames[1] = strTemp2[1].Trim();
                    strError = "";
                    break;
                case (3):
                    strNames[0] = strTemp2[0].Trim() + " " + strTemp2[1].Trim(); 
                    strNames[1] = strTemp2[2].Trim();
                    strError = "";
                    break;
                case (4):
                    strNames[0] = strTemp2[0].Trim() + " " + strTemp2[1].Trim() + " " + strTemp2[2].Trim();
                    strNames[1] = strTemp2[3].Trim();
                    break;
                default:
                    strError = "MORE-THAN-FOUR";
                    break;
            }
        }
        else
        {
            //IF no comma and only with spaces
            string[] strTemp2 = strFullName.Trim().Split(' ');  //Split by space

            switch (strTemp2.Length)
            {
                case (0):   //This case never happened
                    strNames[0] = "";   //first Name
                    strNames[1] = "";   //Middle Name
                    strNames[1] = "";   //Last Name
                    strError = "NO-NAME";
                    break;
                case (1):
                    strNames[0] = strTemp2[0].Trim();
                    strNames[1] = "";
                    strNames[2] = "";
                    strError = "NO-LAST-MIDDLE-NAME";
                    break;
                case (2):
                    strNames[0] = strTemp2[0].Trim();
                    strNames[1] = "";
                    strNames[2] = strTemp2[1].Trim();
                    strError = "";
                    break;
                case (3):
                    strNames[0] = strTemp2[0].Trim();
                    strNames[1] = strTemp2[1].Trim();
                    strNames[2] = strTemp2[2].Trim();
                    strError = "";
                    break;
                case (4):
                    strNames[0] = strTemp2[0].Trim() + " " + strTemp2[1].Trim();
                    strNames[1] = strTemp2[2].Trim();
                    strNames[2] = strTemp2[3].Trim();
                    strError = "";
                    break;
                default:
                    strError = "MORE-THAN-FOUR";
                    break;
            }

        }
        return strNames;
    }

fullname_splitter's People

Contributors

rmpasha avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.