C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Uppercasing the first letter is often necessary. The result string has its first letter uppercased. Its remaining part is unchanged.
Words: Strings with multiple words can be changed to title case. We can capitalize "Multiple Words."
Example. First, the goal of these methods is to return correctly uppercased strings in an efficient way. We provide an example of a static method that uppercases the first letter. The first method here is the slower of the two.
Based on: .NET 4 C# program that uppercases words 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 Samuel Julia John smith
Example 2. Next, we look at a method 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.
C# program that uses ToCharArray, uppercases 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 Samuel Julia John smith
Benchmark. 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 application.
Benchmark results UppercaseFirst method: 0.1636 seconds UppercaseFirst with ToCharArray: 0.0920 seconds [faster]
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.
Uppercase words. This program defines a method named UppercaseWords that is equivalent to the ucwords function in scripting languages such as PHP. The UppercaseWords method internally converts the string to a character array buffer.
C# program that uppercases first letters using System; class Program { static string UppercaseWords(string value) { char[] array = value.ToCharArray(); // Handle the first letter in the string. if (array.Length >= 1) { if (char.IsLower(array[0])) { array[0] = char.ToUpper(array[0]); } } // Scan through the letters, checking for spaces. // ... Uppercase the lowercase letters following spaces. for (int i = 1; i < array.Length; i++) { if (array[i - 1] == ' ') { if (char.IsLower(array[i])) { array[i] = char.ToUpper(array[i]); } } } return new string(array); } static void Main() { // Uppercase words in these strings. const string value1 = "something in the way"; const string value2 = "dot net PERLS"; const string value3 = "String_two;three"; const string value4 = " sam"; // ... Compute the uppercase strings. Console.WriteLine(UppercaseWords(value1)); Console.WriteLine(UppercaseWords(value2)); Console.WriteLine(UppercaseWords(value3)); Console.WriteLine(UppercaseWords(value4)); } } Output Something In The Way Dot Net PERLS String_two;three Sam
The program defines the UppercaseWords static method, which is equivalent to the ucwords function in scripting languages. Internally, the method body of the UppercaseWords method first tests the first character of the string.
It special-cases the first character because the loop in the next part does not correctly allow for the first character. The if-statement is validated in the test cases when the first two strings have their first letters uppercased.
Next: UppercaseWords does a for-loop through all the characters in the string starting at the second character with index of one.
And: In each iteration it tests the previous character for a space. If it detects a space, it modifies the character array buffer.
Efficiency notes. The UppercaseWords method could be implemented by first splitting on the sub-words in the string based on a regular expression or the Split method. But this would result in many more allocations.
However: You could use the Regex.Split method for a better algorithm of breaking up the words.
Note: The method does not contain sophisticated error correction. It may incorrectly handle some words.
ToTitleCase. You might not be in the mood for deploying a custom method for uppercasing the first letter in each word in a string. You can use the ToTitleCase method on the TextInfo type—it does the same thing.
Summary. 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 certain names can be added.