Converts the first character of each word to Uppercase. Example: "the lazy dog" returns "The Lazy Dog" - CSharp System

CSharp examples for System:String Convert

Description

Converts the first character of each word to Uppercase. Example: "the lazy dog" returns "The Lazy Dog"

Demo Code

/*//from   ww  w.  j a v  a2 s  .com
 * @version   : 2.5.0
 * @author    : Ext.NET, Inc. http://www.ext.net/
 * @date      : 2014-10-20
 * @copyright : Copyright (c) 2008-2014, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
 * @license   : See license.txt and http://www.ext.net/license/. 
 * @website   : http://www.ext.net/
 */
using System.Web.UI;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        /// <summary>
        /// Converts the first character of each word to Uppercase. Example: "the lazy dog" returns "The Lazy Dog"
        /// </summary>
        public static string ToTitleCase(this string[] words, CultureInfo ci)
        {
            if (words == null || words.Length == 0)
            {
                return "";
            }

            for (int i = 0; i < words.Length; i++)
            {
                words[i] = (ci != null ? char.ToUpper(words[i][0], ci) : char.ToUpper(words[i][0])) + words[i].Substring(1);
            }

            return string.Join(" ", words);
        }
        /// <summary>
        /// Converts the first character of each word to Uppercase. Example: "the lazy dog" returns "The Lazy Dog"
        /// </summary>
        public static string ToTitleCase(this string[] words)
        {
            return words.ToTitleCase(null);
        }
        /// <summary>
        /// Converts the first character of each word to Uppercase. Example: "the lazy dog" returns "The Lazy Dog"
        /// </summary>
        /// <param name="ci"></param>
        /// <param name="text">The text to convert to sentence case</param>
        public static string ToTitleCase(this string text, CultureInfo ci)
        {
            if (text.IsEmpty())
            {
                return text;
            }

            return text.Split(' ').ToTitleCase(ci);
        }
        /// <summary>
        /// Converts the first character of each word to Uppercase. Example: "the lazy dog" returns "The Lazy Dog"
        /// </summary>
        /// <param name="text">The text to convert to sentence case</param>
        public static string ToTitleCase(this string text)
        {
            if (text.IsEmpty())
            {
                return text;
            }

            return text.Split(' ').ToTitleCase();
        }
        /// <summary>
        /// Determine is the string is null or empty.
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsEmpty(this string text)
        {
            return string.IsNullOrEmpty(text);
        }
        /// <summary>
        /// Join the items together
        /// </summary>
        /// <param name="items">The items to join.</param>
        /// <param name="separator">The separator.</param>
        /// <param name="template">The template to format the items with.</param>
        /// <returns></returns>
        public static string Join(this IEnumerable items, string separator, string template)
        {
            StringBuilder sb = new StringBuilder();

            foreach (object item in items)
            {
                if (item != null)
                {
                    sb.Append(separator);
                    sb.Append(string.Format(template, item.ToString()));
                }
            }

            return sb.ToString().RightOf(separator);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="items"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string Join(this IEnumerable items, string separator)
        {
            return items.Join(separator, "{0}");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public static string Join(this IEnumerable items)
        {
            return items.Join(",", "{0}");
        }
}

Related Tutorials