Home
Manage Your Code
Snippet: Parse Full Name (C#)
Title: Parse Full Name Language: C#
Description: Parse a string for firstname, middleinitial and lastname Views: 5336
Author: Daniel Bardi Date Added: 7/23/2008
Copy Code  
1        /// <summary>

2        /// Parses the full name.

3        /// </summary>

4        /// <param name="FullName">The full name.</param>

5        /// <param name="_firstName">Name of the _first.</param>

6        /// <param name="_middleInitial">The _middle initial.</param>

7        /// <param name="_lastName">Name of the _last.</param>

8        public static void ParseFullName(string FullName, out string _firstName, out string _middleInitial, out string _lastName)
9        {
10            _firstName = "";
11            _middleInitial = "";
12            _lastName = "";
13
14            string[] parts = new string[] { };
15            string[] seperators = new string[1] { " " };
16            parts = FullName.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
17
18            switch (parts.Length)
19            {
20                case 1:
21                    {
22                        // OPTIONS

23                        //

24                        // 0=firstname

25                        //

26                        _firstName = parts[0];      // 0=firstname

27                        break;
28                    }
29                case 2:
30                    {
31                        // OPTIONS:

32                        //

33                        // option1: 0=salutation, 1=lastname

34                        // option2: 0=firstname, 1=lastname

35                        //

36                        if (parts[0].EndsWith(".")) // option2

37                        {
38                            try
39                            {
40                                //TODO: parse salutation    // 0=salutation

41                            }
42                            catch
43                            {
44                                _firstName = parts[0];      // 0=firstname

45                            }
46                            _lastName = parts[1];           // 1=lastname

47                        }
48                        else // option2

49                        {
50                            _firstName = parts[0];          // 0=firstname

51                            _lastName = parts[1];           // 1=lastname

52                        }
53                        break;
54                    }
55                case 3:
56                    {
57                        // OPTIONS:

58                        //

59                        // option1: 0=salutation, 1=firstname, 2=lastname

60                        // option2: 0=firstname, 1=middle, 2=lastname

61                        // option3: 0=firstname, 1=lastname, 3=namesuffix

62                        //

63                        if (parts[0].EndsWith("."))
64                        {
65                            //TODO: parse salutation                                    // 0=salutation

66                            _firstName = parts[1];                                      // 1=firstname

67                            _lastName = parts[2];                                       // 2=lastname

68                        }
69                        else
70                        {
71                            _firstName = parts[0];                                      // 0=firstname

72                            if (parts[1].EndsWith(".") | parts[1].Length == 1)
73                            {
74                                _middleInitial = parts[1].Substring(0, 1).ToUpper();    // 1=middle

75                                _lastName = parts[2];                                   // 2=lastname

76                            }
77                            else
78                            {
79                                if (parts[3].EndsWith(".") | parts[1].Length == 1)
80                                {
81                                    _firstName = parts[0];                              // 0=firstname

82                                    _lastName = parts[1];                               // 1=lastname

83                                    //TODO: parse namesuffix                            // 3=namesuffix

84                                }
85                            }
86                        }
87                        break;
88                    }
89                case 4:
90                    {
91                        // OPTIONS:

92                        //

93                        // option1: 0=salutation, 1=firstname, 2=lastname, 3=namesuffix

94                        // option2: 0=salutation, 1=firstname, 2=middle, 3=lastname

95                        // option3: 0=firstname, 1=middle, 2=lastname, 3=namesuffix

96                        //

97                        if (parts[0].EndsWith(".")) //option1

98                        {
99                            //TODO: parse salutation                                // 0=salutation

100                            _firstName = parts[1];                                  // 1=firstname

101
102                            if (parts[2].EndsWith(".")) //option2

103                            {
104                                _middleInitial = parts[2].Substring(0, 1).ToUpper(); // 2=middleinitial

105                                _lastName = parts[3];                               // 3=lastname

106                            }
107                            else //option1

108                            {
109                                _lastName = parts[2];                               // 2=lastname                            

110                                //TODO: parse namesuffix                            // 3=namesuffix

111                            }
112                        }
113                        else //option3

114                        {
115                            _firstName = parts[0];                                  // 0=firstname

116                            _middleInitial = parts[1].Substring(0, 1).ToUpper();     // 1=middle

117                            _lastName = parts[2];                                   // 2=lastname

118                            //TODO: parse namesuffix                                // 3=namesuffix

119                        }
120                        break;
121                    }
122            }
123        }
124