C# String ToLower() Method
In C#, the ToLower() method is a string method used to convert all uppercase characters in a string to lowercase. If a character does not have a lowercase equivalent, such as a special symbol, it remains unchanged. This method is particularly useful when performing case-insensitive string comparisons or formatting text.
Example 1: Basic Usage of ToLower() Method
// C# program to demonstrate the
// use of ToLower() method
using System;
class Geeks
{
public static void Main()
{
// Original string
string s1 = "GeeksForGeeks";
// Convert string to lowercase
string s2 = s1.ToLower();
// Display results
Console.WriteLine("String before conversion: " + s2);
Console.WriteLine("String after conversion: " + s2);
}
}
Output
String before conversion: geeksforgeeks String after conversion: geeksforgeeks
Explanation: In this example, the method converts all uppercase letters to lowercase while keeping other characters unchanged.
Syntax of String ToLower() Method
This method can be overloaded by passing the different types of arguments to it.
public string ToLower ();
public string ToLower(System.Globalization.CultureInfo culture);
- Parameter: culture: A CultureInfo object that defines culture-specific casing rules.
- Return Type: It returns the string value, which is the lowercase equivalent of the string of type System.String. The second method does the same but according to the specified culture.
- Exception: Throws ArgumentNullException if culture is null.
Example 2: Handling Special Characters
// C# program to demonstrate the
// use of ToLower() method with special characters
using System;
class Geeks
{
public static void Main()
{
// Original string with special symbols
string s1 = "This is C# Program XSDD_$#%";
// Convert to lowercase
string s2 = s1.ToLower();
Console.WriteLine("String before conversion: " + s1);
Console.WriteLine("String after conversion: " + s2);
}
}
Output
String before conversion: This is C# Program XSDD_$#% String after conversion: this is c# program xsdd_$#%
Explanation: In this example, the method converts uppercase letters to lowercase while special symbols remain unchanged.
Example 3: Using ToLower() with CultureInfo
// C# program to demonstrate the
// use of ToLower(CultureInfo) method
using System;
using System.Globalization;
class Geeks
{
public static void Main()
{
// Original string
string s1 = "THIS IS C# PROGRAM XSDD_$#%";
// Convert to lowercase using English-US culture
string s2 = s1.ToLower(new CultureInfo("en-US", false));
Console.WriteLine("Original string: " + s1);
Console.WriteLine("String after conversion: " + s2);
}
}
Output
Original string: THIS IS C# PROGRAM XSDD_$#% String after conversion: this is c# program xsdd_$#%
Explanation: In this example, the method uses culture-specific rules to convert uppercase letters to lowercase.
Important Points:
- The ToLower() method converts all uppercase characters in a string to lowercase.
- Special characters and symbols remain unchanged.
- The method does not modify the original string but returns a new one.
- The overloaded ToLower(CultureInfo) allows culture-specific conversions.