0

example of the column

I have a column called FULLNAME which consists of a variation of FIRSTNAME and SURNAME separated by a space, and TITLE, FIRSTNAME and SURNAME, all separated by a space. So for example, I could have JOHN SMITH or DR JOHN SMITH.

I am using C# in Visual Studios.

I loop through each of these as per:

 foreach (DataRow dr in spreadsheetdata.Tables["EPABase"].Rows)

And my array for the title is as:

            title[0] = "Mr";
            title[1] = "Mrs";
            title[2] = "Miss";
            title[3] = "Ms";
            title[4] = "Dr";
            title[5] = "Rev";
            title[6] = "Hon";
            title[7] = "Mx";
            title[8] = "Other";

It doesn't matter which way around I work, but it's probably easier to get the SURNAME first because it'll always be the the last set of characters in a string, up to the first space value from the right. If I an get this into it's own string of SURNAME, then remove it from the original FULLNAME string, I can then use my array to see if the first set of characters from the left up to the first space appears in it, and if so use as the TITLE string, but if not, use the remaining string after the deletion of the SURNAME to be the FIRSTNAME string.

Just a bit stuck as to how to achieve the first step of this, getting the SURNAME out. I have tried LASTINDEXOF, but this is an integer value, and I need string.

Jay
  • 5
  • 6
  • Show us your input? Impossible to say anything about splitting without seing samples of data. Also do you always have the same order of names, as in Title always first, then Surname, then Firstname? – trailmax Nov 21 '19 at 11:46
  • I've added an image of the column in question. It's called "Contact_Name". As you'll see, if there's a title, it'll always be first, and it's always FIRSTNAME followed by SURNAME whether there's a TITLE in front of not – Jay Nov 21 '19 at 11:52
  • So, what happens when either the first name or surname has a space in it? – AsheraH Nov 21 '19 at 11:57
  • The firstname or surname won't have a space in the middle of it in any scenario – Jay Nov 21 '19 at 12:00
  • If format of the names does not change and you get `{title(optional)} {firstName} {surname}`, then `.Split(" ")` by space, if there are 3 elements after splitting, then first title, second first name, third element is surname. If there are 2 elements after splitting, then first is Firstname, second is Surname. – trailmax Nov 21 '19 at 14:03

2 Answers2

1

If you are sure that First name or Last Name don't have space in it, you try something like this:

string[] fullNames = { "John Smith", "Dr John Smith" };
        string[] titles = { "Mr", "Mrs", "Dr" };

        foreach (var item in fullNames)
        {
            var details = item.Split(' ');
            if (titles.Contains(details[0]))
            {
                Console.WriteLine($"Title: { details[0]} ");
                Console.WriteLine($"First Name: { details[1]} ");
                Console.WriteLine($"Last Name: { details[2]} ");
            }
            else
            {
                Console.WriteLine($"First Name: { details[0]} ");
                Console.WriteLine($"Last Name: { details[1]} ");
            }
        }
rjs123431
  • 688
  • 4
  • 14
  • Thanks for this. For the first line you've put, the name won't always be John Smith or Dr John Smith, so I assume I jut put the string for the column here? – Jay Nov 21 '19 at 12:04
  • That is just a sample implementation. You may need to modify the `foreach` instead. – rjs123431 Nov 21 '19 at 12:12
  • This is brilliant. With some minor adjustments to fit in with my scenario, it works. Thanks so much – Jay Nov 21 '19 at 14:15
0

To get the surname you can do something as follows:

foreach (DataRow dr in spreadsheetdata.Tables["EPABase"].Rows)
        {
            var value = dr["FULLNAME"].ToString();
            var elementsOfName = value.Split(" ");
            var lastName = elementsOfName[elementsOfName.Length - 1];
            Console.WriteLine(lastName);
        }
AimusSage
  • 696
  • 4
  • 5
  • Not quite unfortunately. Where you have, for example Mr John Smith, this picks JOHN up as the SURNAME. Perhaps also worth mentioning is that i'm using a WPF form and not a Console App and possibly because of that, the ^ doesn't work. – Jay Nov 21 '19 at 13:18
  • The [^1] is a C# 8.0 feature. See: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#indices-and-ranges. It allows to get the last element in an Array. I will update the example for you – AimusSage Nov 21 '19 at 13:29