I deal with hundreds and hundreds of names each week. These names (along with other pedigree information) are stored in a database. Typically, I get these names in all sorts of formats, mainly proper and in all-caps. I needed an easy way to convert the names (especially last names) to their proper format:
- McDonald
- MacDougal
- Smith-Jones
- Davis II
- George IV
The following code properly formats the above examples as well as standard names. Please take a look at my code and suggest areas where I might make it more efficient. I got the idea for this method mainly from this link.
public string ConvertToProperNameCase(string input)
{
bool SuffixProcessed = false;
input = input.Trim();
if (String.IsNullOrEmpty(input))
{
return String.Empty;
}
char[] chars = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input.ToLower()).ToCharArray();
for (int i = 0; i + 1 < chars.Length; i++)
{
if ((chars[i].Equals('\'')) ||
(chars[i].Equals('-')))
{
chars[i + 1] = Char.ToUpper(chars[i + 1]);
}
}
string s = new string(chars);
if (((s.ToUpper().StartsWith("MAC") || s.ToUpper().StartsWith("MCC") || s.ToUpper().StartsWith("DE ")) && s.Length > 4))
{
try
{
s = s.Substring(0, 1).ToUpper() + s.Substring(1, 2).ToLower() + s.Substring(3, 1).ToUpper() + s.Substring(4).ToLower();
}
catch
{
s = s;
}
}
if ((s.ToUpper().StartsWith("MC")) && s.Length > 3)
{
try
{
s = s.Substring(0, 1).ToUpper() + s.Substring(1, 1).ToLower() + s.Substring(2, 1).ToUpper() + s.Substring(3).ToLower();
}
catch
{
s = s;
}
}
if (s.ToUpper().Contains(" III") && !SuffixProcessed)
{
try
{
s = s.Substring(0, s.ToUpper().IndexOf(" III")) + " " + s.Substring(s.ToUpper().IndexOf(" III"), 4).ToUpper();
SuffixProcessed = true;
}
catch
{
s = s;
}
}
if (s.ToUpper().Contains(" II") && !SuffixProcessed)
{
try
{
s = s.Substring(0, s.ToUpper().IndexOf(" II")) + " " + s.Substring(s.ToUpper().IndexOf(" II"), 3).ToUpper();
SuffixProcessed = true;
}
catch
{
s = s;
}
}
if (s.ToUpper().Contains(" IV") && !SuffixProcessed)
{
try
{
s = s.Substring(0, s.ToUpper().IndexOf(" IV")) + " " + s.Substring(s.ToUpper().IndexOf(" IV"), 3).ToUpper();
SuffixProcessed = true;
}
catch
{
s = s;
}
}
return s;
}
After carefully digesting Winston Ewert's post, here is what I can up with. This code will now handle the following examples:
- MACDonald-jOnes
- FRAnK iV
- MCCarthy xiv
- mcTavish-tOMaS jr
- Robert-ROBERTSON SR
Revised code
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] Names = new string[]{ "MACDonald-jOnes, FRAnK iV", "MCCarthy xiv", " ", "mcTavish-tOMaS jr", "Robert-ROBERTSON SR."};
foreach (string s in Names)
Console.WriteLine(string.Format("Original Name: {0}\n Converted Name: {1}", s, ConvertToProperNameCase(s)));
}
public string ConvertToProperNameCase(string s)
{
string ReturnValue = string.Empty;
if (s.Trim().Length > 0)
{
try
{
s = s.ToLower();
bool[] Name = new bool[s.Length];
for (int i = Name.Length; i < Name.Length; i++)
{
Name[i] = false;
}
for (int i = 0; i < s.Length; i++)
{
if (i == 0)
{
Name[i] = true;
}
if (s[i].Equals('\'') || s[i].Equals('-') || s[i].Equals(' '))
{
Name[i + 1] = true;
}
}
string[] Prefixes = new string[] { "MAC", "MC" };
bool[] b = new bool[1] { true };
foreach (string p in Prefixes)
{
if (s.ToUpper().Trim().StartsWith(p))
{
switch (p.Length)
{
case 2:
b = new bool[] { true, false, true };
break;
case 3:
b = new bool[] { true, false, false, true };
break;
}
break;
}
}
for (int i = 0; i < b.Length; i++)
{
Name[i] = b[i];
}
string[] Suffixes = new string[] { "XXIIII", "IIIII", "VIIII", "XVIII", "XXIII", "IIII", "VIII", "XIII", "XVII", "XXII",
"XXIV", "III", "VII", "IIX", "XII", "XIV", "XVI", "XIX", "XXV", "XXI", "II", "IV", "VI",
"IX", "XI", "XV", "XX", "I", "V", "X" };
foreach (string suf in Suffixes)
{
if (s.ToUpper().Trim().EndsWith(suf) && (s.Length > (suf.Length + 1)))
{
for (int i = s.Length - 1; i > (s.Length - (suf.Length + 1)); i--)
{
Name[i] = true;
}
break;
}
}
for (int i = 0; i < s.Length; i++)
{
if (Name[i] == true)
{
ReturnValue += s.Substring(i, 1).ToUpper();
}
else
{
ReturnValue += s.Substring(i, 1);
}
}
return ReturnValue;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return ReturnValue = "Error";
}
}
else
{
return ReturnValue = "Empty String";
}
}
}
}