C# String.ToLower() – Examples
String.ToLower() method is used to get a copy of a given string converted to lowercase.
This method does not modify the original string, but returns a new string in which all characters in the given string are converted to lowercase.
In this tutorial, we will learn about the syntax of C# String.ToLower() and C# String.ToLower(CultureInfo) methods, and learn how to use these method with the help of examples.
ToLower()
String.ToLower() returns a copy of this string converted to lowercase.
String.ToLower() throws System.ArgumentNullException if the string is null.
Syntax
The syntax of ToLower() method is
String.ToLower()
Return Value
This method returns String value.
Example 1 – ToLower()
In this example, we will take a string with some upper-case and some lower-case alphabets, say "Hello World"
. To get lower-case of this string we will call ToLower() method on this string. A string with all the lower case characters for the given string should be returned by ToLower() method.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "Hello World";
String result = str.ToLower();
Console.WriteLine($"Original String is : {str}");
Console.WriteLine($"Result of ToLower() : {result}");
}
}
Output
Original String is : Hello World
Result of ToLower() : hello world
Example 2 – ToLower()
In this example, we will take a string with all upper-case alphabets, say ABCDEF
. To get lower-case of this string we will call ToLower() method on this string. A string with all the lower case characters for the given string should be returned.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "ABCDEF";
String result = str.ToLower();
Console.WriteLine($"Original String is : {str}");
Console.WriteLine($"Result of ToLower() : {result}");
}
}
Output
Original String is : ABCDEF
Result of ToLower() : abcdef
Example 3 – ToLower() – Null String
In this example, we will take a null string and call ToLower() method on this null string. ToLower() should throw System.NullReferenceException.
C# Program
using System;
using System.Globalization;
class Example {
static void Main(string[] args) {
String str = null;
String result = str.ToLower();
Console.WriteLine($"Original String is : {str}");
Console.WriteLine($"Result of ToLower() : {result}");
}
}
Output
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Example.Main(String[] args) in D:\workspace\csharp\HelloWorld\Program.cs:line 7
ToLower(CultureInfo)
String.ToLower() returns a copy of this string converted to lowercase, using the casing rules of the specified culture.
Syntax
The syntax of ToLower(CultureInfo) method is
String.ToLower(CultureInfo culture)
where
Parameter | Description |
---|---|
culture | The object that supplies culture-specific casing rules. |
Return Value
This method returns String value.
Example 4 – ToLower(culture)
In this example, we will take a Turkish character in the string. This character is I with dot on top of it. We will convert this string into lower case, with culture information provided as argument to ToLower() method.
C# Program
using System;
using System.Globalization;
class Example {
static void Main(string[] args) {
String str = new String(new Char[] {'\u0130'});
String result = str.ToLower(new CultureInfo("tr-TR", false));
Console.WriteLine($"Original String is : {str}");
Console.WriteLine($"Result of ToLower() : {result}");
}
}
Output
Original String is : I
Result of ToLower() : i
Conclusion
In this C# Tutorial, we have learnt the syntax of C# String.ToLower() method, and also learnt how to use this method with the help of C# example programs.