How to make a first letter capital in C#

后端 未结 12 1191
攒了一身酷
攒了一身酷 2020-12-05 23:52

How can the first letter in a text be set to capital?

Example:

it is a text.  = It is a text.
12条回答
  •  长情又很酷
    2020-12-06 00:10

    polygenelubricants' answer is fine for most cases, but you potentially need to think about cultural issues. Do you want this capitalized in a culture-invariant way, in the current culture, or a specific culture? It can make a big difference in Turkey, for example. So you may want to consider:

    CultureInfo culture = ...;
    text = char.ToUpper(text[0], culture) + text.Substring(1);
    

    or if you prefer methods on String:

    CultureInfo culture = ...;
    text = text.Substring(0, 1).ToUpper(culture) + text.Substring(1);
    

    where culture might be CultureInfo.InvariantCulture, or the current culture etc.

    For more on this problem, see the Turkey Test.

提交回复
热议问题