C# Uppercase First Letter

You want to capitalize your string by uppercasing the first letter, and then return it with the remaining part unchanged. You need the ucfirst function from PHP and Perl. Here we look at ways to uppercase the first letter in strings in the C# programming language.

~~~ C# uppercase first letter timings ~~~            
    It is faster to use ToCharArray with this method.
    1 million iterations tested.                     

UppercaseFirst method:            0.1636 seconds 
UppercaseFirst with ToCharArray:  0.0920 seconds  [faster]

~~~ Uppercase method input and required output ~~~     
    Only the first letter in the string is capitalized.

Input:  samuel
        julia
        john smith

Output: Samuel
        Julia
        John smith [only first letter]

Capitalizing strings

First, the goal in the methods in this article is return correctly uppercased strings in an efficient way. In this article, I am going to show you an example and benchmark of a C# method that uppercases the first letter. The method first here is the slower of the two.

=== Program that uppercases words (C#) ===

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(UppercaseFirst("samuel"));
        Console.WriteLine(UppercaseFirst("julia"));
        Console.WriteLine(UppercaseFirst("john smith"));
    }

    static string UppercaseFirst(string s)
    {
        // Check for empty string.
        if (string.IsNullOrEmpty(s))
        {
            return string.Empty;
        }
        // Return char and concat substring.
        return char.ToUpper(s[0]) + s.Substring(1);
    }
}

=== Output of the program ===

Samuel
Julia
John smith

Alternative method

Here we look at a method written in the C# language and safe code that uses the ToCharArray method instead. I investigated ToCharArray and its performance impact on this sort of character-based method. The following is the faster version, and it returns the same results.

=== Program that uppercases - version 2 (C#) ===

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(UppercaseFirst("samuel"));
        Console.WriteLine(UppercaseFirst("julia"));
        Console.WriteLine(UppercaseFirst("john smith"));
    }

    static string UppercaseFirst(string s)
    {
        if (string.IsNullOrEmpty(s))
        {
            return string.Empty;
        }
        char[] a = s.ToCharArray();
        a[0] = char.ToUpper(a[0]);
        return new string(a);
    }
}

=== Output of the program ===

Samuel
Julia
John smith

Benchmarks

Here are my results from testing 1,000,000 iterations of these functions in a loop over the string "michael". This test is in my ASP.NET application, so the environment is the same as it will be in my ASP.NET app as it is updated on the Internet. See the figures at the top of this article.

Results. The second approach is faster because it only allocates one new string in the return statement. The first approach allocates two strings: the Substring(1), and then a new string with string.Concat.

Uppercasing multiple words

Here we note that it is possible to actually loop through multiple words in a string and uppercase each one that follows a space character or other whitespace character. This site contains a detailed tutorial on uppercasing words in strings, which is an equivalent in the C# language to the ucwords function from scripting languages.

See Uppercase Words in String.

Summary

Here we looked at methods that uppercase the first character in strings using the C# language. They are useful for database results that may not be formatted correctly. Custom logic for names such as "McCain" and "DeGeneres" can be added. Normally, the performance requirements are not strict here, but occasionally the benchmarks here may be useful.

See Lowercase/Uppercase Articles.

See Char Articles.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen