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.
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]} ");
}
}