I had an xls file with lots of full names in the following form at work:
+----------------------------+--------------+-----+-----+
| [Full name] | [More data] |[...]|[...]|
+----------------------------|--------------------------+
| Cristiano RONALDO | ... | ... | ... |
+----------------------------+--------------+-----+-----+
| Carol SEVILLA | ... | ... | ... |
+----------------------------|--------------+-----+-----+
| Ronald Chris MAC DONALDS | ... | ... | ... |
+----------------------------|--------------+-----+-----+
some of the data will still be input this way but I want to add a column for last name and let it clear that I don't need the last name in upper case anymore, so I separated name from last name, and then changed the last name to camel case, notice that last names can have many words like "Mc Donalds Rodriguez" (it happens) so I solved it as follows
public static string GetLastNameFromFullName(string fullName)
{
var lastName = "";
foreach (var ch in fullName)
{
lastName += ch;
if (char.IsLower(ch))
{
lastName = "";
}
}
return lastName.TrimStart();
}
public static string GetCameledLastName(string lastNames)
{
string[] lastNamesArr = lastNames.Split(' ');
var lastNamesCameled = "";
foreach (string lastNameUpper in lastNamesArr)
{
lastNamesCameled += lastNameUpper[0];
for (int i = 1; i < lastNameUpper.Length; i++)
{
lastNamesCameled += char.ToLower(lastNameUpper[i]);
}
}
return lastNamesCameled;
}
public static string GetNameWithoutLastName(string fullName)
{
var possibleLastName = false;
char possibleLastNameChar = ' '; //just initialized
var name = "";
foreach (var ch in fullName)
{
if (char.IsUpper(ch))
{
possibleLastNameChar = ch;
if (possibleLastName)
{
break;
}
possibleLastName = true;
}
else
{
if (possibleLastName)
{
name += possibleLastNameChar;
}
name += ch;
possibleLastName = false;
}
}
return name;
}
private void Form1_Load(object sender, EventArgs e)
{
var path = @"../../file.txt"; //dumped from xls file
string contents = File.ReadAllText(path);
using (StreamReader reader = new StreamReader(path, Encoding.GetEncoding("iso-8859-1"))) //some names had ñ or accented characters
{
string line;
while ((line = reader.ReadLine()) != null)
{
var fullName = "";
foreach (char ch in line)
{
if (ch == '\t')
{
//The columns in the xls file were divided by tab characters
}
else
{
fullName += ch;
}
}
var lastName = GetLastNameFromFullName(fullName);
Console.WriteLine("Name: " + GetNameWithoutLastName(fullName));
Console.WriteLine("Last name: " + GetCameledLastName(lastName));
}
}
}
I think my code could be a lot better.
UPDATE: please note that while it is true that cases like "Cinthia del Río" is an actual name that is not considered in this way, it will be converted to "Cinthia Del Rio" because in the xls file it would be in a single column as "Cinthia DEL RIO", and of course it is impossible for the algorithm to know that "DEL" should actually be "del" even though it is perfectly OK for a last name's word to start with a lower case.
CultureInfo.CurrentCulture.TextInfo.ToTitleCase(fullName.ToLower())
\$\endgroup\$