Strings sometimes have lowercase first letters. Uppercasing the first letter is often necessary for example, a name. The code samples are examples of how to make a string's first letter uppercase using String.Substring() method of C# and .NET.
The string has its first letter uppercased. It is then returned with the remaining part unchanged. This is the functionality of the ucfirst function from PHP and Perl. And strings with multiple words can be changed to title case.
First Example
Input
amit patel
ranna patel
- public static string FirstCharToUpper(string s)
- {
-
- if (string.IsNullOrEmpty(s))
- {
- return string.Empty;
- }
-
- return char.ToUpper(s[0]) + s.Substring(1);
- }
OUTPUT
Amit patel
Ranna patel
Secont Example
Input
amit patel
ranna patel
- public static string FirstCharToUpper(string value)
- {
- char[] array = value.ToCharArray();
-
- if (array.Length >= 1)
- {
- if (char.IsLower(array[0]))
- {
- array[0] = char.ToUpper(array[0]);
- }
- }
-
-
- 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);
- }
OUTPUT
Amit Patel
Ranna Patel