How to make a first letter capital in C#

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);
}

It'll be something like this:

// precondition: before must not be an empty string

String after = before.Substring(0, 1).ToUpper() + before.Substring(1);

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.

Tags:

C#