Possible Duplicate:
How to make a first letter capital in C#
I am trying to capitalize the first word in a sentence. This is what I have, but it is not working.
char.ToUpper(sentence[0]) + sentence.Substring(1)
JaredPar's solution is right, but I'd also like to point you towards the TextInfo class. ToTitleCase() will capitalize the first letter, and convert the remaining to lower-case.
string s = "heLLo";
var t = new CultureInfo("en-US", false).TextInfo;
s = t.ToTitleCase(s); // Prints "Hello"
It sounds like you're just trying to capitalize the first character of a string
value. If so then your code is just fine, but you need to assign the new string back into the sentence
value.
sentence = char.ToUpper(sentence[0]) + sentence.Substring(1)
A string
in .NET is immutable and hence every operation which changes the string
produces a new value. It won't change the original value in place. So in order to see the result of the change, you must assign it into a variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With