How to uppercase the first letter of a word or sentence

When I used to use PHP to develop my web applications, I really loved its basic but useful method named ucfirst. This function is explained using a simple explanation on PHP documentation:

ucfirstMake a string’s first character uppercase

I believe within all those useful and complex libraries taking place in .NET Framework, it still misses these kind of basic and useful methods. However, the good side is that we can extend it using our custom extension methods.

Because I needed to implement such a feature as PHP offers using ucfirst function, I wrote the following simple extension to achieve the same functionality.

/// <summary>
/// Capitalizes the first letter of the word.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ToUpperFirst(this string value)
    if (string.IsNullOrEmpty(value))
        return string.Empty;

    if (value.Length > 1)
                value.Substring(0, 1).ToUpper(),
        return value.ToUpper();

And this is how to use it:

string val = "something";
val = val.ToUpperFirst(); // result = Something

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.