Solved

C# ProperCase function that handles McDonalde

Posted on 2004-10-03
2
903 Views
Last Modified: 2008-01-09
I need a ProperCase function that handles McDonald and ONeils?
I have a 'standard' on. Does any one have one that converts  MCDONALD to McDonald ?
TIA
0
Comment
Question by:JElster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
der_jth earned 75 total points
ID: 12212933
I didn't have one, but writing it was an interesting challenge :-) It certainly isn't one of the fastest, but it's probably one of the most readable and configurable ones. Tailor to your needs. The algorithm isn't exactly perfect, but I couldn't come up with a situation where the flaws would've mattered in practice.


using System.Text.RegularExpressions;
...

// CONFIGURATION:

// The following words will always be in lower case (except in the start of the string)
static string[] lowerCaseWords = { "of", "the", "and", "or", "a", "an", "von" };

// The following prefixes will cause their next character to be uppercased
// Note: Keep the first character uppercase when defining these; all else must be in lowercase
static string[] upperCasePrefixes = { "Mc", "O'" };

// The following words will be always presented in the case they have here.
static string[] fixedCaseWords = { "USA", "NATO", "Mbit" };



/// <summary>
/// Converts the given string into ProperCase.
/// </summary>
/// <param name="original">The original string, f.e. "THE EYE OF THE TIGER"</param>
/// <returns>The string converted into ProperCase, f.e. "The Eye of the Tiger"</returns>
public static string ProperCase(string original) {

  if (original == null || original.Length == 0) return "";

  // Run the original through the massage word-by-word
  string result =
    Regex.Replace(original.ToLower(), @"\b(\w+)\b", new MatchEvaluator(HandleSingleWord));

  // Always uppercase the first character
  return Char.ToUpper(result[0]) + (result.Length > 1 ? result.Substring(1) : "");
}


// This helper method properizes (sp?) the case of a single word (regex match)
// NOTE: The input is in all lowercase as forced by the ProperCase method.
private static string HandleSingleWord(Match m) {

  string word = m.Groups[1].Value;

  // Is this word defined as all-lowercase?
  foreach (string lcw in lowerCaseWords)
    if (word == lcw)
      return word;

  // Is this word defined as a fixed-case word?
  foreach (string fcw in fixedCaseWords)
    if (String.Compare(word, fcw, true) == 0)
      return fcw;

  // Ok, this is a normal word; uppercase the first letter
  if (word.Length == 1)
    return Char.ToUpper(word[0]).ToString();
  word = Char.ToUpper(word[0]) + word.Substring(1);

  // Check if this word starts with one of the uppercasing prefixes
  // Note: Only one of the uppercasing prefixes is applies
  foreach (string ucPrefix in upperCasePrefixes)
    if (word.StartsWith(ucPrefix) && word.Length > ucPrefix.Length)
      return word.Substring(0, ucPrefix.Length) +
              Char.ToUpper(word[ucPrefix.Length]) +
              (word.Length > ucPrefix.Length + 1
                ? word.Substring(ucPrefix.Length + 1)
                : "");

  return word;
}
0
 
LVL 6

Expert Comment

by:der_jth
ID: 12212949
Actually, that Mbit was a stupid example. Substitute "MHz" to see some real effect. :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question