Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# ProperCase function that handles McDonalde

Posted on 2004-10-03
2
Medium Priority
?
909 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 300 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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