Summary: in this tutorial, you will learn how to use the C# String ToLower()
method to convert a string to lowercase.
Introduction to the C# ToLower() method
The String ToLower()
method returns a copy of the current string converted to lowercase. Here’s the syntax of the ToLower()
method:
public string ToLower();
Code language: PHP (php)
For example, the following program uses the ToLower()
method to convert the message "Hello, World!"
to lowercase:
using static System.Console;
var message = "Hello, World!";
var lower = message.ToLower();
WriteLine(lower);
Code language: C# (cs)
Output:
hello, world!
By default, the ToLower()
method considers the case rule of the current culture. If you want to convert a string to lowercase using a specific culture, you can use the following overload of the ToLower()
method:
public string ToLower (
System.Globalization.CultureInfo? culture
);
Code language: CSS (css)
In this syntax, the culture
is an instance of CultureInfo
that specifies a culture-specific casing rule.
The following example demonstrates how using different CultureInfo
objects can lead to different lowercase conversions based on specific language rules:
using System.Globalization;
using static System.Console;
var s = "Istanbul İZMİR Ankara";
// Using the default (English) culture
var englishLowercase = s.ToLower(CultureInfo.InvariantCulture);
WriteLine("Default (English) lowercase: " + englishLowercase);
// Using the Turkish culture
var turkishCulture = new CultureInfo("tr-TR");
var turkishLowercase = s.ToLower(turkishCulture);
Console.WriteLine("Turkish lowercase: " + turkishLowercase);
Code language: JavaScript (javascript)
In this example, we have a string that contains Turkish characters, such as "İ"
(capital i
with a dot) and "İ"
(small i
with a dot) which are specific to the Turkish alphabet.
When we use the ToLower()
to convert a string to lowercase using the default culture (English), the method uses the English rules for lowercase conversion, and we get "istanbul İZMİR ankara"
.
However, when we use the ToLower()
method with the Turkish culture ("tr-TR"
), the method uses Turkish-specific lowercase rules for conversion.
In Turkish, the lowercase equivalent of "İ"
is "i"
without a dot, and the lowercase equivalent of "I"
is "ı"
(a dotless i). As a result, the Turkish lowercase string becomes "i̇stanbul i̇zmir ankara"
.
C# String ToLowerInvariant()
The String ToLowerInvariant()
returns a lowercase version of a string using the casing rules of the invariant culture.
Here’s the syntax of the ToLowerInvariant()
method:
public string ToLowerInvariant();
Code language: PHP (php)
The ToLowerInvariant()
method is equivalent to the ToLower()
method with the CultureInfo.InvariantCulture
argument:
ToLower(CultureInfo.InvariantCulture)
Code language: CSS (css)
One practical example of using the ToLowerInvariant()
method is when you need to perform a case-sensitive string comparison.
Using ToLowerInvariant()
ensures consistent behavior and avoids potential issues with culture-specific rules.
The following example demonstrates how to use the ToLowerInvariant()
method to check if the input username is in a list:
using static System.Console;
// Get the username from input
Write("Enter you username:");
var searchUsername = ReadLine();
// Username list
var usernames = new List<string>{ "JohnDoe", "JaneSmith", "MikeJohnson", "SarahBrown" };
// Check if the username exists
var userExists = usernames.Exists(
username => username.ToLowerInvariant() == searchUsername?.ToLowerInvariant()
);
// Set the message
var message = userExists
? $"The user {searchUsername} exists in the list."
: $"The user {searchUsername} does not exist in the list.";
WriteLine(message);
Code language: JavaScript (javascript)
Output:
Enter you username:johndoe
The user johndoe exists in the list.
Code language: CSS (css)
Summary
- Use the C# String
ToLower()
method to convert a string to lowercase.