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: 444

Filed under:
|
|

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:

  1. JOHN SMITH -> John Smith
  2. SMITH, JOHN T -> Smith, John T
  3. JOHN O'BRIAN -> John O'Brian
  4. JOHN DOE-SMITH -> John Doe-Smith

There are some edge cases that do no work like:

  1. JASON MCDONALD -> Jason Mcdonald (Correct: Jason McDonald)
  2. OSCAR DE LA HOYA -> Oscar De La Hoya (Correct: Oscar de la Hoya)
  3. 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

Related posts about c#

Related posts about uppercase