首页 / C#开发 / 正文

[C#].NET/C#程序开发中怎么将一个单词或者一个字符的首字母大写,哪些方式更高效呢?

2530 发布于: 2018-03-02 读完约需10分钟

.NET&&C#程序开发

问题描述

在.NET/C#程序开发,有时候需要将一个单词或者一个字符的首字母大写,比如:

"red" --> "Red"
"red house" --> " Red house"

应该如何实现,又有哪些更高效的实现方法呢?

方案一

将单词或者字符串首字母大写最高效的方式是将字符串先转换成字符数组,然后使用new string(...),如:

public static string FirstLetterToUpperCase(this string s)
    {
        if (string.IsNullOrEmpty(s))
            throw new ArgumentException("There is no first letter");

        char[] a = s.ToCharArray();
        a[0] = char.ToUpper(a[0]);
        return new string(a);
    }

方案二

使用string.First()以及string.Substring()方法实现,如:

public static string FirstCharToUpper(string input)
{
    switch (input)
    {
        case null: throw new ArgumentNullException(nameof(input));
        case "": throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input));
        default: return input.First().ToString().ToUpper() + input.Substring(1);
    }
}

或者:

public static string FirstCharToUpper(string input)
{
    if (String.IsNullOrEmpty(input))
        throw new ArgumentException("ARGH!");
    return input.First().ToString().ToUpper() + input.Substring(1);
}

或者

public static string FirstCharToUpper(string input)
{
    if (String.IsNullOrEmpty(input))
        throw new ArgumentException("ARGH!");
    return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
}

方案三

public string FirstLetterToUpper(string str)
{
    if (str == null)
        return null;

    if (str.Length > 1)
        return char.ToUpper(str[0]) + str.Substring(1);

    return str.ToUpper();
}

或者使用CultureInfo.CurrentCulture.TextInfo.ToTitleCase()方法,如:

public string ToTitleCase(string str)
{
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
}

方案四

使用ToTitleCase(...)方法,并创建一个静态扩展方法,如:

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

namespace Test
{
    public static class StringHelper
    {
        private static CultureInfo ci = new CultureInfo("en-US");
        //Convert all first latter
        public static string ToTitleCase(this string str)
        {
            str = str.ToLower();
            var strArray = str.Split(' ');
            if (strArray.Length > 1)
            {
                strArray[0] = ci.TextInfo.ToTitleCase(strArray[0]);
                return string.Join(" ", strArray);
            }
            return ci.TextInfo.ToTitleCase(str);
        }
        public static string ToTitleCase(this string str, TitleCase tcase)
        {
            str = str.ToLower();
            switch (tcase)
            {
                case TitleCase.First:
                    var strArray = str.Split(' ');
                    if (strArray.Length > 1)
                    {
                        strArray[0] = ci.TextInfo.ToTitleCase(strArray[0]);
                        return string.Join(" ", strArray);
                    }
                    break;
                case TitleCase.All:
                    return ci.TextInfo.ToTitleCase(str);
                default:
                    break;
            }
            return ci.TextInfo.ToTitleCase(str);
        }
    }

    public enum TitleCase
    {
        First,
        All
    }
}

调用示例:

string str = "red house";
str.ToTitleCase();
//输出结果 : Red house

string str = "red house";
str.ToTitleCase(TitleCase.All);
//输出结果 : Red House

方案五

使用正则表达式来实现,如下面的静态扩展方法:

static public string UpperCaseFirstCharacter(this string text) {
    return Regex.Replace(text, "^[a-z]", m => m.Value.ToUpper());
}

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

上一篇: [C#].NET/C#程序开发中如何修改存储在字典(Dictionary)中的值呢?

下一篇: [SQL Server]SQL Server中有哪些更好的分页的方法呢?

本文永久链接码友网 » [C#].NET/C#程序开发中怎么将一个单词或者一个字符的首字母大写,哪些方式更高效呢?

分享扩散:

发表评论

登录用户才能发表评论, 请 登 录 或者 注册