Make first letter of a string upper case (with maximum performance)

I have a DetailsView with a TextBox and I want the input data be saved always with the FIRST LETTER IN CAPITAL.

Example:

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

How can I achieve this maximizing performance ?


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


NOTE :
The question is ambiguous as to whether letters after the first should be forced to lower case. The accepted answer assumes that only the first letter should be altered. If you want to force all letters in the string except the first to be lower case, look for an answer containing ToLower , and not containing ToTitleCase.


EDIT: Updated to newer syntax (and more correct answer)

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);
    }
}

OLD ANSWERS

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

EDIT : This version is shorter. For a faster solution take a look at Equiso's answer

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

EDIT 2 : Probably the fastest solution is Darren's (There's even a benchmark) although I would change it's string.IsNullOrEmpty(s) validation to throw an exception since the original requirement expects for a first letter to exist so it can be uppercased. Note that this code works for a generic string and not particularly on valid values from the Textbox .


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();
}

老答案:这使每个第一个字母大写

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

正确的方法是使用文化:

Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word.ToLower())
链接地址: http://www.djcxy.com/p/21028.html

上一篇: 为什么我们必须在C#中定义==和!=?

下一篇: 使字符串大写的第一个字母(具有最高性能)