C# string ToLower - Convert string to lowercase

c# string tolower

While writing C# code some times we need a string to be converted to lowercase string, so that the uppercase letters of the string get converted to lowercase letters. This is useful when you need to compare the string with lowercase string. It is good practice to make comparing strings to be convert to uppercase or lowercase if we don’t know how input is coming. We will see how to convert all letters to small case in the string using ToLower function in C#.

C# code that shows how to use ToLower [C#]


using System;
class Program
{
 static void Main()
 {
  string str = "C# STRING LOWERCASE EXAMPLE";
  string upStr = str.ToLower();
  Console.WriteLine(upStr);
 }
}

Result
c# string lowercase example


The ToLower function or method converts all the uppercase characters in the string to lowercase characters, If string contains some lowercase letters or characters it ignores them. We will see how to compare two strings by using lowercase to the input string.

using System;

class Program
{
 static void Main()
 {
  string str = "C# STRING LOWERCASE";
  string upStr = str.ToLower();
  if("c# string lowercase” == upStr)
  Console.WriteLine("Strings are equal.");
 }
}


Result
Strings are equal.

C# string ToUpper - Convert string to uppercase

c# string toupper

In some cases we need to convert lowercase string to uppercase string, so that the lowercase letters of the string get converted  to uppercase letters. We can uppercase string using c# string ToUpper Method This is useful when you need to compare the string with uppercase string. It is good practice to make comparing strings to be convert to uppercase or lowercase if we don’t know how input is coming. We will see how to capitalize all letters in the string using ToUpper function in C#.


C# code that shows how to use ToUpper [C#]


using System;

class Program
{
static void Main()
{
string str = "C# string uppercase example";
string upStr = str.ToUpper();
Console.WriteLine(upStr);
}
}

//Result
//C# STRING UPPERCASE EXAMPLE


The ToUpper function or method converts all the lowercase letters in the string to uppercase letters, If string contains some uppercase letters or characters it ignores them. We will see how to compare two strings by using uppercase the the input string.


using System;

class Program
{
static void Main()
{
string str = "C# string uppercase";
string upStr = str.ToUpper();
if("C# STRING UPPERCASE” == upStr)
Console.WriteLine("Strings are equal.");
}
}

//Result
//Strings are equal.

Some time you may need to just upper case the first latter or character of the string. In that case we can use ToCharArray method and the for first character user char.ToUpper().