
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.
Uppercase method input and required output Input: samuel julia john smith Output: Samuel Julia John smith [only first letter]
The goal in the methods in this article is to 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
Samuel
Julia
John smithHere 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.
Another program that uppercases [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
Samuel
Julia
John smithHere 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.
Benchmark results UppercaseFirst method: 0.1636 seconds UppercaseFirst with ToCharArray: 0.0920 seconds [faster]
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.
You can also use the ToTitleCase method to uppercase the first letter in your string. If there are additional words in the string, these words will also have their first letters uppercased. The behavior is not precisely equivalent to the methods implemented above.
ToTitleCase Method
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.
Uppercase WordsWe 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.
String Type