Add title case to string class : Extension « Class « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    public static class ExtensionLibExtensions
    {
        public static string ToTitleCase(this string inputString, bool forceLower)
        {
            inputString = inputString.Trim();
            if (inputString == "")
            {
                return "";
            }
            if (forceLower)
            {
                inputString = inputString.ToLower();
            }

            string[] inputStringAsArray = inputString.Split(' ');
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < inputStringAsArray.Length; i++)
            {
                if (inputStringAsArray[i].Length > 0)
                {
                    sb.AppendFormat("{0}{1} ",
                       inputStringAsArray[i].Substring(0, 1).ToUpper(),
                       inputStringAsArray[i].Substring(1));
                }
            }
            return sb.ToString(0, sb.Length - 1);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string sourceString = "this is a test";
            Console.WriteLine("String with title casing: {0}", sourceString.ToTitleCase(true));
        }
    }








7.60.Extension
7.60.1.Adding extension method for int
7.60.2.Adding extension to Stream
7.60.3.Adding reverse operation to string
7.60.4.Add title case to string class
7.60.5.Extension Method On Null Reference
7.60.6.extension method
7.60.7.conflicting extension methods
7.60.8.Extension Methods