Saturday, 20 April 2013

How to make a first letter capital in C#


How to make a first letter capital in C#


How can the first letter in a word of sentence using c#.
as an example

it is a text.  = It is a text.

solution is use below function.

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);
}
another good solution is 

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

so you can use above solution.