Capitalizes the specified string. - CSharp System

CSharp examples for System:String Case

Description

Capitalizes the specified string.

Demo Code

//   The MIT License (MIT)
using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System;/*from   w w w  .  j a v  a 2 s . c o m*/

public class Main{
        /// <summary>
        /// Capitalizes the specified string.
        /// </summary>
        /// <param name="string">The string.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>capitalized string.</returns>
        public static string Capitalize(string @string, CultureInfo culture = null)
        {
            if (string.IsNullOrEmpty(@string))
            {
                return string.Empty;
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            if (@string.Length == 1)
            {
                return @string.ToUpper(culture);
            }

            return @string.Substring(0, 1).ToUpper(culture) + @string.Substring(1);
        }
}

Related Tutorials