我有一个带有文本框的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的答案。


当前回答

这也可以使用Take, Skip和Aggregate:

    public static string FirstCharToUpper(this string text) 
    {
      if (String.IsNullOrEmpty(text)) return String.Empty;
      var first = text.Take(1).ToArray()[0].ToString().ToUpper();
      var rest = text.Skip(1).Aggregate("", ((xs, x) => xs + x));
      return first + rest;
    }

其他回答

下面的函数对所有方式都是正确的:

static string UppercaseWords(string value)
{
    char[] array = value.ToCharArray();
    // Handle the first letter in the string.
    if (array.Length >= 1)
    {
        if (char.IsLower(array[0]))
        {
            array[0] = char.ToUpper(array[0]);
        }
    }
    // Scan through the letters, checking for spaces.
    // ... Uppercase the lowercase letters following spaces.
    for (int i = 1; i < array.Length; i++)
    {
        if (array[i - 1] == ' ')
        {
            if (char.IsLower(array[i]))
            {
                array[i] = char.ToUpper(array[i]);
            }
        }
    }
    return new string(array);
}

我在这里找到的。

如果性能/内存使用是一个问题,那么它只创建一个(1)StringBuilder和一个(1)与原始字符串相同大小的新String。

public static string ToUpperFirst(this string str) {
  if(!string.IsNullOrEmpty(str)) {
    StringBuilder sb = new StringBuilder(str);
    sb[0] = char.ToUpper(sb[0]);

    return sb.ToString();

  } else return str;
}

我想提供一个“最大性能”的答案。在我看来,“最大性能”的答案抓住了所有的场景,并提供了解释这些场景的问题的答案。所以,这是我的答案。原因如下:

IsNullOrWhiteSpace accounts for strings that are just spaces or null/empty. .Trim() removes white space from the front and back of the string. .First() takes the first element of an IEnumerable<TSource> (or string). We should check to see if it is a letter that can/should be uppercase. We then add the rest of the string, only if the length indicates we should. By .NET best practice, we should provide a culture under System.Globalization.CultureInfo. Providing them as optional parameters makes this method totally reusable, without having to type the chosen culture every time. I also noticed that my and most of these answers did not maintain the whitespace at the beginning of the string. This will also show how to maintain that whitespace. //Capitalize the first letter disregard all chars using regex. public static string RegCapString(this string instring, string culture = "en-US", bool useSystem = false) { if (string.IsNullOrWhiteSpace(instring)) { return instring; } var m = Regex.Match(instring, "[A-Za-z]").Index; return instring.Substring(0, m) + instring[m].ToString().ToUpper(new CultureInfo(culture, useSystem)) + instring.Substring(m + 1); } //Capitalize first char if it is a letter disregard white space. public static string CapString(this string instring, string culture = "en-US", bool useSystem = false) { if (string.IsNullOrWhiteSpace(instring) || !char.IsLetter(instring.Trim().First())) { return instring; } var whiteSpaces = instring.Length - instring.TrimStart().Length; return (new string(' ', whiteSpaces)) + instring.Trim().First().ToString().ToUpper(new CultureInfo(culture, useSystem)) + ((instring.TrimStart().Length > 1) ? instring.Substring(whiteSpaces + 1) : ""); }

正如BobBeechey在回答这个问题时所建议的,下面的代码可以解决这个问题:

private void txt_fname_TextChanged(object sender, EventArgs e)
{
    char[] c = txt_fname.Text.ToCharArray();
    int j;
    for (j = 0; j < txt_fname.Text.Length; j++)
    {
        if (j==0) c[j]=c[j].ToString().ToUpper()[0];
        else c[j] = c[j].ToString().ToLower()[0];
    }
    txt_fname.Text = new string(c); 
    txt_fname.Select(txt_fname.Text.Length, 1);
}

不同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)验证来抛出异常,因为最初的需求期望第一个字母存在,以便它可以大写。注意,这段代码适用于一般字符串,而不是文本框中的有效值。