The Char.ToLowerInvariant(Char) function in C# uses the invariant culture to convert a specific Unicode character to its lowercase equivalent. It is a part of the Char class. This method is incredibly helpful when you want to manipulate characters without being affected by their cultural settings.
Syntax:
It has the following syntax:
public static char ToLowerInvariant(char c);
Access Modifier:
public: The visibility of a method may be specified using the public access modifier. In this case, the method may be accessed from any other code within the same assembly and external assemblies.
Static Keyword :
static: Static indicates that the method belongs to the type rather than an instance of the type. In other words, you can call this method without creating an instance of the class that contains it.
Return Type:
char: Char is the data type of the value the method returns. In this case, the method returns a single Unicode character.
Method Name:
ToLowerInvariant: ToLowerInvariant is the name of the method. This method is intended for converting a Unicode character to its lowercase equivalent using the invariant culture.
Example 1:
Let us take an example to illustrate the Char.ToLowerInvariant(Char) method in C#.
using System;
class Demo
{
static void Main()
{
char original_Char1 = 'S';
char lowercase_Char1 = Char.ToLowerInvariant(original_Char1);
Console.WriteLine($"Original Char is: {original_Char1}, Lowercase Char is: {lowercase_Char1}");
char original_Char2 = 'X';
char lowercase_Char2 = Char.ToLowerInvariant(original_Char2);
Console.WriteLine($"Original Char is: {original_Char2}, Lowercase Char is: {lowercase_Char2}");
char original_Char3 = '3';
char lowercase_Char3 = Char.ToLowerInvariant(original_Char3);
Console.WriteLine($"Original Char is: {original_Char3}, Lowercase Char is: {lowercase_Char3}");
string input_String = "HeLloWorlD";
string lowercase_String = ConvertToLowerCaseInvariant(input_String);
Console.WriteLine($"Original String is: {input_String}, Lowercase String is: {lowercase_String}");
}
static string ConvertToLowerCaseInvariant(string input)
{
char[] char_Array = input.ToCharArray();
for (int i = 0; i < char_Array.Length; i++)
{
char_Array[i] = Char.ToLowerInvariant(char_Array[i]);
}
return new string(char_Array);
}
}
Output:
Original Char is: S, Lowercase Char is: s
Original Char is: X, Lowercase Char is: x
Original Char is: 3, Lowercase Char is: 3
Original String is: HeLloWorlD, Lowercase String is: helloworld
Explanation:
- Character Conversion:
originalChar1, originalChar2 , and original_Char3 are declared and initialized with the characters 'S', 'X', and '3'.
After that, each of these characters is converted to lowercase using Char.ToLowerInvariant, the output is kept in lowercaseChar1, lowercaseChar2, and lowercase_Char3.
- Displaying Results:
"Console.WriteLine" is used to display the character conversion results.
- String Conversion:
Using "Char.ToLowerInvariant" for each character, ConvertToLowerCaseInvariant takes a string (input) as an argument and changes it to lowercase. The method returns the modified string.
- Displaying String Results:
"Console.WriteLine" displays both the original and lowercase versions of the input string. The program demonstrates how to convert characters to their lowercase equivalents using Char.ToLowerInvariant, and applies this to both individual characters and an entire string.
Example 2:
Let us take another example to illustrate the Char.ToLowerInvariant(Char) method in C#.
using System;
class Demo
{
public static void Main()
{
get('Q');
get('y');
get('F');
get('6');
}
public static void get(char c)
{
//Obtaining the Unicode character
// making use of the ToLowerInvariant() Method
char ch = Char.ToLowerInvariant(c);
// displaying the char value
Console.WriteLine("The lowercase equivalent value"+
" of the {0} is {1}", c, ch);
}
}
Output:
The lowercase equivalent value of the Q is q
The lowercase equivalent value of the y is y
The lowercase equivalent value of the F is f
The lowercase equivalent value of the 6 is 6
Explanation:
- Main Method:
The program's entry point is the Main method.
Using a different character as an argument, it makes four calls to the get method.
- Get method:
The get method requires a char parameter called 'c' .
The lowercase equivalent of the input character 'c' is obtained within the method using the Char.ToLowerInvariant method.
The lowercase equivalent of the input character 'c' is obtained within the method using the Char.ToLowerInvariant method. This method uses the invariant culture's casing rules to convert a given Unicode character to its lowercase equivalent.
The lowercase character is stored in the variable 'ch' .
After that, a message is displayed to the console using "Console.WriteLine", showing the original character 'c' and its lowercase equivalent 'ch' .
- Output:
'Q', 'y', 'F', and '6' are the four distinct characters that used when the Main method uses the get method four times.
The program displays a message for each call, including the original character and its lowercase equivalent.
The application shows how to convert characters to their lowercase equivalents using Char.ToLowerInvariant, and then prints the results for each character that is passed to the get method.