Possible Duplicates:
Capitalizing word in a string
Make first letter upper case
I have a string like this:
var a = "this is a string";
Is there a simple way I can make the first character change to uppercase?
This is a string
Possible Duplicates:
Capitalizing word in a string
Make first letter upper case
I have a string like this:
var a = "this is a string";
Is there a simple way I can make the first character change to uppercase?
This is a string
You can use the following code:
if (!String.IsNullOrEmpty(a))
a = Char.ToUpper(a[0]) + a.Substring(1);
If you're sure that the string won't be null or empty, you can drop the if
statement as well but I prefer to program defensively.
If you are not worried about the fact that a string is immutable, than you can return a new string instance.
var a = "this is a string";
a = string.Format("{0}{1}", char.ToUpper(a[0]), a.Remove(0, 1));
But, if you are going to end up needing to do more string manipulation on the same value, you may want to consider using a StringBuilder instead.
var a = "this is a string";
StringBuilder builder = new StringBuilder(a);
builder.Replace(a[0], char.ToUpper(a[0]), 0, 1);