The ToLower()
method in C# is a string method that converts all the characters in a string to lowercase and returns a new string with the converted characters. The original string remains unchanged.
Here’s an example of how to use the ToLower()
method:
string originalString = "Hello World!"; string lowerString = originalString.ToLower(); Console.WriteLine(lowerString); // Output: hello world! Console.WriteLine(originalString); // Output: Hello World!
In the example above, the ToLower()
method is called on the originalString
, which converts all the characters to lowercase and returns a new string assigned to the lowerString
variable. The Console.WriteLine()
statements demonstrate the original string and the new lowercase string.
Signature:
The ToLower()
method in C# has the following signature:
public string ToLower();
The method does not take any parameters. It operates on the instance of the string
class on which it is called and returns a new string with all characters converted to lowercase.
Here’s an example of using the ToLower()
method with its signature:
string originalString = "Hello World!"; string lowerString = originalString.ToLower();
In the example above, ToLower()
is called on the originalString
, and the converted lowercase string is stored in the lowerString
variable.
Parameter:
The ToLower()
method in C# does not accept any parameters. It is a parameterless method that operates on an instance of the string
class. When called on a string object, it converts all the characters within that string to lowercase and returns a new string with the converted characters.
Here’s an example of using the ToLower()
method without any parameters:
string originalString = "Hello World!"; string lowerString = originalString.ToLower();
In this example, the ToLower()
method is called on the originalString
object. It does not require any additional information, and it converts all the characters in the string to lowercase. The resulting lowercase string is assigned to the lowerString
variable.
Return:
The ToLower()
method in C# returns a new string that is a copy of the original string with all characters converted to lowercase. The original string remains unchanged.
Here’s an example to demonstrate the return value of the ToLower()
method:
string originalString = "Hello World!"; string lowerString = originalString.ToLower(); Console.WriteLine(lowerString); // Output: hello world!
In the example above, the ToLower()
method is called on the originalString
. It returns a new string with all characters converted to lowercase, which is then assigned to the lowerString
variable. The Console.WriteLine()
statement outputs the converted lowercase string.
C# String ToLower() Method Example:
Certainly! Here’s an example that demonstrates the usage of the ToLower()
method in C#:
string originalString = "Hello World!"; string lowercaseString = originalString.ToLower(); Console.WriteLine("Original String: " + originalString); Console.WriteLine("Lowercase String: " + lowercaseString);
Output:
Original String: Hello World! Lowercase String: hello world!
In the example above, we have a string variable originalString
with the value “Hello World!”. We call the ToLower()
method on originalString
to convert all the characters to lowercase, and the resulting lowercase string is stored in the lowercaseString
variable.
Finally, we use Console.WriteLine()
to display both the original and lowercase strings on the console. As you can see in the output, the ToLower()
method successfully converts the original string to lowercase.