Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Convert Character case
Converting character case in C# is a common string manipulation task. The string class provides built-in methods ToLower() and ToUpper() to convert strings to lowercase and uppercase respectively.
Syntax
Following is the syntax for converting string case −
string.ToLower() // Converts to lowercase string.ToUpper() // Converts to uppercase
Both methods return a new string with the case converted and do not modify the original string.
Using ToLower() Method
The ToLower() method converts all uppercase characters in a string to lowercase −
using System;
class Program {
static void Main(string[] args) {
string str = "AMIT";
Console.WriteLine("Original: {0}", str);
Console.WriteLine("Converted to LowerCase: {0}", str.ToLower());
}
}
The output of the above code is −
Original: AMIT Converted to LowerCase: amit
Using ToUpper() Method
The ToUpper() method converts all lowercase characters in a string to uppercase −
using System;
class Program {
static void Main(string[] args) {
string str = "hello world";
Console.WriteLine("Original: {0}", str);
Console.WriteLine("Converted to UpperCase: {0}", str.ToUpper());
}
}
The output of the above code is −
Original: hello world Converted to UpperCase: HELLO WORLD
Converting Mixed Case Strings
Both methods work with mixed case strings, converting only the relevant characters −
using System;
class Program {
static void Main(string[] args) {
string mixedCase = "Hello World 123!";
Console.WriteLine("Original: {0}", mixedCase);
Console.WriteLine("ToLower(): {0}", mixedCase.ToLower());
Console.WriteLine("ToUpper(): {0}", mixedCase.ToUpper());
}
}
The output of the above code is −
Original: Hello World 123! ToLower(): hello world 123! ToUpper(): HELLO WORLD 123!
Converting Individual Characters
For individual characters, use char.ToLower() and char.ToUpper() methods −
using System;
class Program {
static void Main(string[] args) {
char upperChar = 'A';
char lowerChar = 'z';
Console.WriteLine("Original chars: {0}, {1}", upperChar, lowerChar);
Console.WriteLine("After conversion: {0}, {1}",
char.ToLower(upperChar),
char.ToUpper(lowerChar));
}
}
The output of the above code is −
Original chars: A, z After conversion: a, Z
Key Points
Case conversion methods return a new string and do not modify the original.
Numbers and special characters remain unchanged during case conversion.
These methods are culture-aware and use the current culture settings by default.
For culture-specific conversion, use overloads like
ToLower(CultureInfo).
Conclusion
C# provides simple and efficient methods for converting character case using ToLower() and ToUpper() for strings, and char.ToLower() and char.ToUpper() for individual characters. These methods return new instances and preserve non-alphabetic characters unchanged.
