C# string.ToLower method explanation with example:
ToLower method is used to convert a string to lowercase in C#. This is a method in the String class and we can use it to convert a string to lowercase in C#.
Strings are consecutive characters and it is immutable, i.e. we can’t modify a string. If we need to do any change in a string, we need to create a different string.
If you want to convert a string to lowercase, you can do it by iterating over the characters of the string one by one, converting each of these characters to uppercase and by joining all to a new string. This method works, but instead, we can use the ToLower method which is already defined in String class.
Definition of ToLower:
ToLower is defined as like below:
public string ToLower()
public string ToLower (System.Globalization.CultureInfo? culture)
- This method returns a copy of the string converted to lowercase, on which we are calling this method.
- We can also pass one CultureInfo to this method to convert the string based on the rules of a specific culture.
- If a string with all lowercase characters is passed, it will return the same string.
Example of ToLower:
Let’s try ToLower with different types of strings:
using System;
public class Program
{
public static void Main()
{
string [] givenStr = {"Hello", "world", "123", "HelloWorld", "##$@"};
foreach (string s in givenStr)
Console.WriteLine("{0} => {1}", s, s.ToLower());
}
}
Here,
- givenStr is an array of strings
- we are using a foreach loop to iterate over the strings in this array and calling ToLower method on each word of this array.
If you run this program, it will print the below output:
Hello => hello
world => world
123 => 123
HelloWorld => helloworld
##$@ => ##$@
Example of ToLower with CultureInfo:
We can also use ToLower with a CultureInfo:
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
string [] info = {"Hello", "world", "123", "HelloWorld", "##$@"};
foreach (string s in info)
Console.WriteLine("{0} => {1}", s, s.ToLower(new CultureInfo("en-US", false)));
}
}
It will print:
Hello => hello
world => world
123 => 123
HelloWorld => helloworld
##$@ => ##$@
Exception:
It will throw ArgumentNullException if we pass a null value as its parameter.
For example, the below code will throw ArgumentNullException:
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
string [] info = {"Hello", "world", "123", "HelloWorld", "##$@"};
foreach (string s in info)
Console.WriteLine("{0} => {1}", s, s.ToLower(null));
}
}
It will throw a Run-time exception:
Run-time exception (line 11): Value cannot be null.
Parameter name: culture
Stack Trace:
[System.ArgumentNullExcep
You might also like:
- C# program to find the circumference of a circle
- C# program to get the first N characters of a string
- C# program to get the DateTime for the start of the week
- C# program to convert a double to integer
- How to print tomorrow’s datetime in C#
- How to print yesterday’s date in C#
- C# program to check if a number is armstrong or not
- C# program to remove first n characters from a string