Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Convert Text to Title Case (Smart)

Description
This macro will convert a string to Title Case, keeping articles and prepositions lower case.
Language
C#.net
Minimum Version
Created By
Patrick Kenzie (BFS)
Contributors
-
Date Created
Aug 19, 2014
Date Last Modified
Aug 19, 2014

Macro Code

using System;
using System.Collections.Generic;

public static class ClipboardFusionHelper
{
    public static string ProcessText(string text)
    {
        //Words that will not be capitalized; add words to this list as required
        string[] exceptionsArray = { "a", "an", "and", "any", "at", "from", "into", "of", "on", "or", "the", "to", };
      
        List<string> exceptions = new List<string>(exceptionsArray);
      
        //Break the input text into a list of capitalized (not upper case) words
        text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(text.ToLower());
        List<string> words = new List<string>(text.Split(new char[] { ' ' }));

        //Always leave the first word capitalized, regardless of what it is
        text = words[0];
        words.RemoveAt(0);
        
        //Check each remaining word against the list, and append it to the new text
        foreach (string s in words)
        {
            if (exceptions.Contains(s.ToLower()))
               text += " " + s.ToLower();    
            else 
               text += " " + s;
        }

        return text;
    }
}