To convert the first character in a string to uppercase in C#, you can use the ToUpper()
method along with string concatenation.
In this example,
str1
with a value of "hello world"
.str1
to uppercase using the ToUpper()
method.using System;
class Program
{
static void Main()
{
string str1 = "hello world";
string modifiedStr1 = char.ToUpper(str1[0]) + str1.Substring(1);
Console.WriteLine("Modified string is: " + modifiedStr1);
}
}
Modified string is: Hello world
In this example,
str2
with a value of "csharp"
.str2
to uppercase using the ToUpper()
method.using System;
class Program
{
static void Main()
{
string str2 = "csharp";
string modifiedStr2 = char.ToUpper(str2[0]) + str2.Substring(1);
Console.WriteLine("Modified string is: " + modifiedStr2);
}
}
Modified string is: Csharp
In this tutorial, we learned How to convert First Character in String to Uppercase in C# language with well detailed examples.