Uppercase and Lowercase Strings in C#

Introduction

The String.ToUpper() and String.ToLower() methods in C# and .NET convert a string into an uppercase and a lowercase string respectively. These methods are easy to use.

The following code sample converts strings into uppercase and lowercase strings. 

string author = "Mahesh Chand";  
string bio = "Mahesh Chand is a founder of C# Corner.";  
  
// Covert everything to uppercase  
string ucaseAuthor = author.ToUpper();          
Console.WriteLine($"Uppercase: {ucaseAuthor}");  
  
// Covert everything to lowercase  
string lcaseAuthor = author.ToLower();  
Console.WriteLine($"Lowercase: {lcaseAuthor}");  
  
// We can direct convert to uppercase or lowercase  
Console.WriteLine(bio.ToLower());  
Console.WriteLine(bio.ToUpper());  

The output from the above code is shown below.

 

Convert the first letter of a string to uppercase

The following code snippet makes a string's first letter uppercase.  

// convert first letter of a string uppercase  
string name = "chris love";  
if (!string.IsNullOrEmpty(name))  
{  
    name = char.ToUpper(name[0]) + name.Substring(1);  
}  
Console.WriteLine($"String with first letter uppercase: {name} ");  

Convert a part of a string to lowercase

The following code snippet converts a part of a string to lowercase.  

// Convert a part of string to lowercase  
string uName = "MAHESH CHAND";  
string updatedName = uName.Substring(0, 1).ToUpper() + uName.Substring(1).ToLower();  
Console.WriteLine(updatedName);  

Make every first character of each word in a string uppercase

TextInfo.ToTitleCase method can be used to make every word in a string starts with an uppercase character.  

// Every word start with uppercase  
string aName = "mahesh chand beniwal";  
string fullName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(aName);  
Console.WriteLine(fullName);  

Conclusion

To learn more about C# strings, check out the String In C# tutorial to learn more about strings and how to work with strings in C#.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.