A Preposition Configurable ASP.NET C# Title Case Method

 
Written by Mark Pringle |
Published on:

ASP.NET has a built-in title case method. However, if you have used this method, you understand that it may yield different results than you want. Here is a custom title case method that allows you to add the prepositions you wish to include as lowercase. There are approximately 150 different prepositions in the English language, but you may not want to include all of them. This method skips the first word of the sentence and does not apply the lowercase to the first word. However, this method does not take into account all uppercase words that are meant to be acronyms. 

        public static string ToTitleCase(string text)
        {
            ArrayList Prepositions = new ArrayList();
            //add the prepositions you want to include to the array. There are approximately 150 different prepositions in the English language.
            Prepositions.Add("the");
            Prepositions.Add("of");
            Prepositions.Add("and");
            Prepositions.Add("at");
            Prepositions.Add("by");
            Prepositions.Add("in");
            Prepositions.Add("into");
            Prepositions.Add("of");
            Prepositions.Add("on");
            Prepositions.Add("to");
            Prepositions.Add("is");
            Prepositions.Add("a");

            string[] words = text.Split(" ");

            for (var i = 0; i < words.Length; i++)
            {
                if (i > 0 && Prepositions.Contains(words[i].ToLower()))
                {
                    words[i] = words[i].ToLower();
                }
                else
                {
                    words[i] =
                      words[i].Substring(0, 1).ToUpper() +
                      words[i].Substring(1).ToLower();
                }
            }
            return string.Join(" ", words);
        }

Input: "guardians of the galaxy is one of the best marvel movies."

Output: "Guardians of the Galaxy is One of the Best Marvel Movies."

Input: "GUARDIANS OF THE GALAXY IS ONE OF THE BEST MARVEL MOVIES."

Output: "Guardians of the Galaxy is One of the Best Marvel Movies."

You can also separate the "preposition finder" and "title case converter" into their own methods to create modular code.

        public static string AlterCase(string text)
        {
            string[] words = text.Split(" ");

            for (var i = 0; i < words.Length; i++)
            {
                if (i > 0 && IsPrepostion(words[i]))
                {
                    words[i] = words[i].ToLower();
                }
                else
                {
                    words[i] = ToTitleCase(words[i]);
                }
            }
            return string.Join(" ", words);
        }

        private static string ToTitleCase(string word)
        {
           return (word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower());
        }

        private static bool IsPrepostion(string word)
        {
            ArrayList Prepositions = new ArrayList();
            //add the prepositions you want to include to the array. There are approximately 150 different prepositions in the English language.
            Prepositions.Add("the");
            Prepositions.Add("of");
            Prepositions.Add("and");
            Prepositions.Add("at");
            Prepositions.Add("by");
            Prepositions.Add("in");
            Prepositions.Add("into");
            Prepositions.Add("of");
            Prepositions.Add("on");
            Prepositions.Add("to");
            Prepositions.Add("is");
            Prepositions.Add("a");

            return Prepositions.Contains(word.ToLower());
        }

 

Copyright © TravelDailyLife.com

Author: Mark Pringle
I'm just a guy who is addicted to the unfamiliar and who fulfills this addiction by traveling and writing about my travels. As a lover of sports, penning opinion articles related to sports is also a pastime.
My External Website (External Website Opens in New Window)

Comments

Please Login to Comment
No comments have been posted. Be the first.



Hire a Writer