Can this method to convert a name to proper case be improved?
Posted
by Kelsey
on Stack Overflow
See other posts from Stack Overflow
or by Kelsey
Published on 2010-04-30T16:19:30Z
Indexed on
2010/04/30
16:27 UTC
Read the original article
Hit count: 450
I am writing a basic function to convert millions of names (one time batch process) from their current form, which is all upper case, to a proper mixed case. I came up with the following so far:
public string ConvertToProperNameCase(string input)
{
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
char[] chars = 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]);
}
}
return new string(chars);;
}
It works in most cases such as:
- JOHN SMITH -> John Smith
- SMITH, JOHN T -> Smith, John T
- JOHN O'BRIAN -> John O'Brian
- JOHN DOE-SMITH -> John Doe-Smith
There are some edge cases that do no work like:
- JASON MCDONALD -> Jason Mcdonald (Correct: Jason McDonald)
- OSCAR DE LA HOYA -> Oscar De La Hoya (Correct: Oscar de la Hoya)
- MARIE DIFRANCO -> Marie Difranco (Correct: Marie DiFranco)
These are not captured and I am not sure if I can handle all these odd edge cases. Can anyone think of anything I could change or add to capture more edge case? I am sure there are tons of edge cases I am not even thinking of as well. All casing should following North American conventions too meaning that if certain countries expect a specific capitalization format, and that differs from the North American format, then the North American format takes precedence.
© Stack Overflow or respective owner