Skip to main content

How to Convert a String to Lowercase with ToLowerInvariant in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

To treat Tokyo, TOKYO and tokyo as the same answer, turn them into lowercase first. ToLowerInvariant does that with the same rules on every computer, so the result doesn't depend on the language the computer is set to.

Microsoft's reference: Returns a copy of this String object converted to lowercase using the casing rules of the invariant culture. The invariant culture is a fixed set of rules that never changes. Plain ToLower() uses the computer's own culture instead, and those rules differ: in Turkish, a capital I doesn't become i.

Syntax​

Syntax
string <lower> = <text>.ToLowerInvariant();
PartWhat it is
<text>The string to convert
ToLowerInvariant()Lowercases it with the invariant culture's rules, the same everywhere
<lower>A new string in lowercase. <text> itself doesn't change

The reference says when to choose it: If your application depends on the case of a string changing in a predictable way that is unaffected by the current culture, use the ToLowerInvariant method.

The Example​

A console app called LowerCase checks five typed answers against the city tokyo, whatever case they were typed in.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n LowerCase
cd LowerCase
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/LowerCase/LowerCase.csproj:
Determining projects to restore...
Restored /Users/you/source/LowerCase/LowerCase.csproj (in 55 ms).
Restore succeeded.

dotnet new console -n LowerCase creates a folder named LowerCase with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or LowerCase.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the answers:

Program.cs (the answers)
string[] answers = ["Tokyo", "TOKYO", "tokyo", "ToKyO", "Kyoto"];

Then the check:

Program.cs (the check)
foreach (string answer in answers)
{
// Lowercase the answer the same way on every computer, then compare it.
string lower = answer.ToLowerInvariant();
Console.WriteLine($"{answer,-6} -> {lower,-6} is Tokyo: {lower == "tokyo"}");
}
  • answer.ToLowerInvariant() gives the answer in lowercase.
  • lower == "tokyo" compares it with the lowercase word, so the case the answer was typed in no longer matters.
  • {answer,-6} pads the text to 6 characters, so the columns line up.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Tokyo -> tokyo is Tokyo: True
TOKYO -> tokyo is Tokyo: True
tokyo -> tokyo is Tokyo: True
ToKyO -> tokyo is Tokyo: True
Kyoto -> kyoto is Tokyo: False

macOS-style terminal window titled Terminal — LowerCase showing dotnet run: Tokyo, TOKYO, tokyo and ToKyO all become tokyo and match Tokyo, while Kyoto becomes kyoto and doesn't

Four spellings of the same word became tokyo and matched. Kyoto has the same letters in another order, so it didn't.

The Complete File​

macOS-style code window titled Program.cs — LowerCase showing five answers and a foreach loop that lowercases each one with ToLowerInvariant and prints whether it equals tokyo

Program.cs
string[] answers = ["Tokyo", "TOKYO", "tokyo", "ToKyO", "Kyoto"];

foreach (string answer in answers)
{
// Lowercase the answer the same way on every computer, then compare it.
string lower = answer.ToLowerInvariant();
Console.WriteLine($"{answer,-6} -> {lower,-6} is Tokyo: {lower == "tokyo"}");
}

Common Mistakes​

MistakeWhat happensDo this instead
Using ToLower() to compare textIt follows the culture's rules. With Turkish rules, "TITLE".ToLower(new CultureInfo("tr-TR")) returned tıtle, with a dotless ı, while "TITLE".ToLowerInvariant() returned titleUse ToLowerInvariant() for anything that isn't shown to a person
Expecting the string itself to changeanswer.ToLowerInvariant(); on its own left answer as TOKYOKeep the result in a variable

FAQ​

Can I compare without lowercasing at all?​

Yes: string.Equals("Tokyo", "TOKYO", StringComparison.OrdinalIgnoreCase) returned True. Microsoft's best practices: Use StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.

Lowercase or uppercase, for comparing?​

Microsoft's best practices prefer uppercase: Use the String.ToUpperInvariant method instead of the String.ToLowerInvariant method when you normalize strings for comparison. For a one-off comparison, OrdinalIgnoreCase avoids the question.

Conclusion​

text.ToLowerInvariant() lowercases text with the same rules on every computer, so a check like answer.ToLowerInvariant() == "tokyo" behaves the same everywhere. When you only need to compare two strings, string.Equals(a, b, StringComparison.OrdinalIgnoreCase) does it without making new strings.

Additional Resources​