C# String - ToLower() Method



The C# String ToLower() method converts all character in a string to lower case and is used to perform case-insensitive operations, normalize strings for comparison, or handle user input.

Syntax

Following are the syntax of the C# string ToLower() method −

Default Syntax −

This syntax return copy of the current string converted to lower case.

public string ToLower();

Parametrised Syntax −

This syntax returns a copy of this string converted to lowercase, using the casing rules of the specified culture.

public string ToLower (System.Globalization.CultureInfo? culture);

Parameters

This method accepts the following parameters −

culture: This is an object that supplies culture-specific wrapper rules. If culture is null, the current culture is used.

Return Value

This method returns a string that is in lowercase equivalent to the current string.

Example 1: Using ToLower with Current Culture

Following is a basic example of the ToLower() method to convert the string into lowercase with the current culture −

    
using System;
class Program {
   public static void Main() {
      string str = "Hello, tpians!";
      string ToLower = str.ToLower();
      Console.Write("Converted String: "+ ToLower);
   }
}

Output

Following is the output −

Converted String: hello, tpians!

Example 2: Using ToLower(CultureInfo) with a specific culture

Let's look at another example. Here, we use the parametrised ToLower() method to convert a string into lowercase with specific culture −

using System;
using System.Globalization;
class Program {
   static void Main() {
      // Turkish character ''
      string str = "stanbul";
      
      //use InvariantCulture
      string lowerInvariant = str.ToLower(CultureInfo.InvariantCulture);
      
      string lowerTurkish = str.ToLower(new CultureInfo("tr-TR"));
   
      Console.WriteLine(lowerInvariant);
      Console.WriteLine(lowerTurkish);
   }
}	

Output

Following is the output −

stanbul
istanbul

Example 3: Case-Insensitive Comparisons

In this example, we convert the string into lowercase using the ToLower() method. We then compare string −

using System;
class Program {
   public static void Main() {
      string userInput = "Admin";
      if (userInput.ToLower() == "admin") {
         Console.WriteLine("Access granted!");
      }
   }
}

Output

Following is the output −

Access granted!
csharp_strings.htm
Advertisements