How to make a first letter capital in C#

66,498

Solution 1

public static string ToUpperFirstLetter(this string source)
{
    if (string.IsNullOrEmpty(source))
        return string.Empty;
    // convert to char array of the string
    char[] letters = source.ToCharArray();
    // upper case the first char
    letters[0] = char.ToUpper(letters[0]);
    // return the array made of the new char array
    return new string(letters);
}

Solution 2

It'll be something like this:

// precondition: before must not be an empty string
String after = before.Substring(0, 1).ToUpper() + before.Substring(1);

Solution 3

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.

Solution 4

If you are using C# then try this code:

Microsoft.VisualBasic.StrConv(sourceString, Microsoft.VisualBasic.vbProperCase)

Solution 5

I use this variant:

 private string FirstLetterCapital(string str)
        {
            return Char.ToUpper(str[0]) + str.Remove(0, 1);            
        }
Share:
66,498

Related videos on Youtube

Peter Mortensen
Author by

Peter Mortensen

Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and & for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)

Updated on July 09, 2022

Comments

  • Peter Mortensen
    Peter Mortensen 5 months

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

    Example:

    it is a text.  = It is a text.
    
    • polygenelubricants
      polygenelubricants over 12 years
      Can the string be something like "99c waffles" and you want to get "99c Waffles"? That is, what is your definition of "first letter"?
    • Jeroen
      Jeroen almost 9 years
  • Alastair Pitts
    Alastair Pitts over 12 years
    You don't need to do the source.ToCharArray(). A string is already a char array and can be indexed with just source[0].
  • Alastair Pitts
    Alastair Pitts over 12 years
    This is ugly. If your using C#, why would you be importing/referencing the Visual Basic DLL?
  • Jacob
    Jacob over 12 years
    @Alastair, strings are immutable but char arrays aren't.
  • Alastair Pitts
    Alastair Pitts over 12 years
    @Jacob, Oooh yeah. Shows how dead my brain is on a Friday afternoon :/
  • Cheng Chen
    Cheng Chen over 12 years
    Consider about different CultrueInfos, as well as TextInfos.
  • Matt Mills
    Matt Mills over 12 years
    @Jon Skeet - That was entirely my intent :) I was actually wondering what the performance would be compared to the substring method above.
  • polygenelubricants
    polygenelubricants over 12 years
    @Danny: let's discuss your comment here stackoverflow.com/questions/3474578/…
  • Vladislav Rastrusny
    Vladislav Rastrusny over 12 years
    .NET is also slow. C++ or assembler is much faster. Does that mean we need to drop .NET and move to faster languages?
  • Jon Skeet
    Jon Skeet over 12 years
    @steven: You can use VB libraries from C#.
  • Jon Skeet
    Jon Skeet over 12 years
    @Danny: Substring makes the code simple. I prefer to write simple code first, and then optimize with more complicated code only where necessary.
  • Admin
    Admin over 12 years
    tell me the most case when we need to import Microsoft.VisualBasic dll when we work on C#.net
  • Yves M.
    Yves M. about 9 years
    Can I suggest you to write if (string.IsNullOrEmpty(source)) return source; instead of string.Empty? :D
  • Yves M.
    Yves M. about 9 years
    And also renaming the method from ToUpperFirstLetter to ToUpperFirstCharacter which is a more relevant name :)
  • Wim Ombelets
    Wim Ombelets almost 9 years
    char[] nm = "this is a test".ToCharArray(); will work
  • Murat Yıldız
    Murat Yıldız almost 8 years
    Taking consideration "CultureInfo" has made the solution perfect... Thanks a lot also for Turkey Test :)
  • haroonxml
    haroonxml over 5 years
    Microsoft.VisualBasic.Strings.StrConv("hello", VbStrConv.ProperCase);
  • General Grievance
    General Grievance over 1 year
    It's wrong anyway. This capitalizes every word, not just the first. Here's the VB function in action Try it online!

Related