Strings sometimes have lowercase first letters. Uppercasing the first letter is often necessary. 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)
{
// 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
Amit patel
Ranna patel
Secont Example
Input
amit patel
ranna patel
public static string FirstCharToUpper(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);
}
OUTPUT
Amit Patel
Ranna Patel