How to make a first letter capital in C#
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);
}
Related videos on Youtube

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, 2022Comments
-
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 over 12 yearsCan the string be something like
"99c waffles"
and you want to get"99c Waffles"
? That is, what is your definition of "first letter"? -
Jeroen almost 9 yearspossible duplicate of Make first letter of a string upper case
-
-
Alastair Pitts over 12 yearsYou don't need to do the
source.ToCharArray()
. A string is already a char array and can be indexed with justsource[0]
. -
Alastair Pitts over 12 yearsThis is ugly. If your using C#, why would you be importing/referencing the Visual Basic DLL?
-
Jacob over 12 years@Alastair, strings are immutable but char arrays aren't.
-
Alastair Pitts over 12 years@Jacob, Oooh yeah. Shows how dead my brain is on a Friday afternoon :/
-
Cheng Chen over 12 yearsConsider about different CultrueInfos, as well as TextInfos.
-
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 over 12 years@Danny: let's discuss your comment here stackoverflow.com/questions/3474578/…
-
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 over 12 years@steven: You can use VB libraries from C#.
-
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 over 12 yearstell me the most case when we need to import Microsoft.VisualBasic dll when we work on C#.net
-
Yves M. about 9 yearsCan I suggest you to write
if (string.IsNullOrEmpty(source)) return source;
instead ofstring.Empty
? :D -
Yves M. about 9 yearsAnd also renaming the method from
ToUpperFirstLetter
toToUpperFirstCharacter
which is a more relevant name :) -
Wim Ombelets almost 9 yearschar[] nm = "this is a test".ToCharArray(); will work
-
Murat Yıldız almost 8 yearsTaking consideration "CultureInfo" has made the solution perfect... Thanks a lot also for Turkey Test :)
-
haroonxml over 5 yearsMicrosoft.VisualBasic.Strings.StrConv("hello", VbStrConv.ProperCase);
-
General Grievance over 1 yearIt's wrong anyway. This capitalizes every word, not just the first. Here's the VB function in action Try it online!