我有一个带有文本框的DetailsView 我希望输入的数据总是以大写的第一个字母保存。

例子:

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

我怎样才能实现性能最大化呢?


注意:

Based on the answers and the comments under the answers, many people think this is asking about capitalizing all words in the string. E.g. => Red House It isn't, but if that is what you seek, look for one of the answers that uses TextInfo's ToTitleCase method. (Note: Those answers are incorrect for the question actually asked.) See TextInfo.ToTitleCase documentation for caveats (doesn't touch all-caps words - they are considered acronyms; may lowercase letters in middle of words that "shouldn't" be lowered, e.g., "McDonald" → "Mcdonald"; not guaranteed to handle all culture-specific subtleties re capitalization rules.)


注意:

第一个字母之后的字母是否必须小写,这个问题很模糊。公认的答案假定只有第一个字母需要修改。如果要强制字符串中除第一个字母外的所有字母都小写,请查找包含ToLower且不包含ToTitleCase的答案。


当前回答

检查字符串是否为空,将第一个字符转换为大写字母,其余字符转换为小写字母:

public static string FirstCharToUpper(string str)
{
    return str?.First().ToString().ToUpper() + str?.Substring(1).ToLower();
}

其他回答

FluentSharp有lowerCaseFirstLetter方法来做这个。

这是最快的方法:

public static unsafe void ToUpperFirst(this string str)
{
    if (str == null)
        return;
    fixed (char* ptr = str)
        *ptr = char.ToUpper(*ptr);
}

在不改变原始字符串的情况下:

public static unsafe string ToUpperFirst(this string str)
{
    if (str == null)
        return null;
    string ret = string.Copy(str);
    fixed (char* ptr = ret)
        *ptr = char.ToUpper(*ptr);
    return ret;
}

我用这个来纠正名字。它的基本原理是,如果字符遵循特定的模式,则将其更改为大写。在这种情况下,我选择了空格和“Mc”的破折号。

private String CorrectName(String name)
{
    List<String> StringsToCapitalizeAfter = new List<String>() { " ", "-", "Mc" };
    StringBuilder NameBuilder = new StringBuilder();
    name.Select(c => c.ToString()).ToList().ForEach(c =>
    {
        c = c.ToLower();
        StringsToCapitalizeAfter.ForEach(s =>
        {
            if(String.IsNullOrEmpty(NameBuilder.ToString()) ||
               NameBuilder.ToString().EndsWith(s))
            {
                c = c.ToUpper();
            }
        });
        NameBuilder.Append(c);
    });
    return NameBuilder.ToString();
}

不同c#版本的解决方案

c# 8,至少。net Core 3.0或。net Standard 2.1

public static class StringExtensions
{
    public static string FirstCharToUpper(this string input) =>
        input switch
        {
            null => throw new ArgumentNullException(nameof(input)),
            "" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
            _ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1))
        };
}

自从。net Core 3.0 / . net Standard 2.1以来,String.Concat()支持ReadonlySpan<char>,如果我们使用. asspan(1)而不是. substring(1),就节省了一个分配。

C# 8

public static class StringExtensions
{
    public static string FirstCharToUpper(this string input) =>
        input switch
        {
            null => throw new ArgumentNullException(nameof(input)),
            "" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
            _ => input[0].ToString().ToUpper() + input.Substring(1)
        };
}

C# 7

public static class StringExtensions
{
    public static string FirstCharToUpper(this 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[0].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 static string FirstCharToUpper(string input)
{
    if (String.IsNullOrEmpty(input))
        throw new ArgumentException("ARGH!");
    return input.First().ToString().ToUpper() + input.Substring(1);
}

可能最快的解决方案是Darren的(甚至有一个基准测试),尽管我会改变它的string.IsNullOrEmpty(s)验证来抛出异常,因为最初的需求期望第一个字母存在,以便它可以大写。注意,这段代码适用于一般字符串,而不是文本框中的有效值。

因为我碰巧也在做这方面的工作,并且四处寻找任何想法,这就是我想到的解决方案。它使用LINQ,并且能够将字符串的第一个字母大写,即使第一个字母不是字母。这是我最后做的扩展方法。

public static string CaptalizeFirstLetter(this string data)
{
    var chars = data.ToCharArray();

    // Find the Index of the first letter
    var charac = data.First(char.IsLetter);
    var i = data.IndexOf(charac);

    // capitalize that letter
    chars[i] = char.ToUpper(chars[i]);

    return new string(chars);
}

我相信有一种方法可以优化或者清理一下。