Convert first letter of a string to uppercase in C#
This post will discuss how to convert the first letter of a string to uppercase in C#. In order words, capitalize a string in C#.
1. Using String.Substring() method
The idea is to extract the first letter from the string and convert it to uppercase by invoking the ToUpper() method, and concatenate the uppercase character with the remaining string. The following sample illustrates this.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; public static class StringExtensions { public static string Capitalize(this string s) { if (String.IsNullOrEmpty(s)) { throw new ArgumentException("String is mull or empty"); } return s[0].ToString().ToUpper() + s.Substring(1); } } public class Example { public static void Main() { string s = "hello"; s = s.Capitalize(); Console.WriteLine(s); // Hello } } |
Don’t forget to check for the empty string, otherwise IndexOutOfRangeException will be thrown. If you need to convert the first letter of a string to uppercase and the remaining characters to lower case, you can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Globalization; public static class StringExtensions { public static string toUpperFirst(this string s) { if (String.IsNullOrEmpty(s)) { return s; } return char.ToUpper(s[0]) + s.Substring(1).ToLower(); } } public class Example { public static void Main() { string s = "heLLo"; s = s.toUpperFirst(); Console.WriteLine(s); // Hello } } |
2. Using AsSpan() method
Alternately, you can use the AsSpan() method over Substring() method. It creates a new read-only span over a string, starting from the specified position till its end.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; public static class StringExtensions { public static string Capitalize(this string s) { if (String.IsNullOrEmpty(s)) { throw new ArgumentException("String is mull or empty"); } return string.Concat(s[0].ToString().ToUpper(), s.AsSpan(1)); } } public class Example { public static void Main() { string s = "hello"; s = s.Capitalize(); Console.WriteLine(s); // Hello } } |
3. Using TextInfo.ToTitleCase() method
If you need to convert your string to a title case, you can use the TextInfo.ToTitleCase() method. In the title case, all words are capitalized.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Globalization; public static class StringExtensions { public static string ToTitleCase(this string s) { TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; return textInfo.ToTitleCase(s); } } public class Example { public static void Main() { string s = "hello world"; s = s.ToTitleCase(); Console.WriteLine(s); // Hello World } } |
To get the culture-independent invariant, you can use the CultureInfo.InvariantCulture property.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Globalization; public static class StringExtensions { public static string ToInvariantCultureTitleCase(this string s) { if (String.IsNullOrEmpty(s)) { return s; } return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s); } } public class Example { public static void Main() { string s = "hello"; s = s.ToInvariantCultureTitleCase(); Console.WriteLine(s); // Hello } } |
That’s all about converting the first letter of a string to uppercase in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)