Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. string.ToUpper() / ToLower() / Trim()

string.ToUpper() / ToLower() / Trim()

The ToUpper() method converts all characters in a string to uppercase, ToLower() converts them to lowercase, and Trim() removes leading and trailing whitespace.

Syntax

// Returns a new string with all characters converted to uppercase.
string.ToUpper()

// Returns a new string with all characters converted to lowercase.
string.ToLower()

// Returns a new string with all leading and trailing whitespace (spaces, tabs, newlines) removed.
string.Trim()

// Removes leading whitespace only.
string.TrimStart()

// Removes trailing whitespace only.
string.TrimEnd()

Method List

MethodDescription
ToUpper()Returns a new string with all alphabetic characters converted to uppercase.
ToLower()Returns a new string with all alphabetic characters converted to lowercase.
Trim()Returns a new string with all leading and trailing whitespace characters (spaces, tabs, newlines, etc.) removed.
TrimStart()Returns a new string with leading whitespace characters removed.
TrimEnd()Returns a new string with trailing whitespace characters removed.

Sample Code

using System;

string langName = "csharp";

// Convert to uppercase using ToUpper().
Console.WriteLine(langName.ToUpper()); // CSHARP

// Convert to lowercase using ToLower().
string input = "HELLO World";
Console.WriteLine(input.ToLower()); // hello world

// Case-insensitive comparison.
string userInput = "Yes";
if (userInput.ToLower() == "yes")
{
    Console.WriteLine("You selected yes."); // This branch executes.
}

// Remove surrounding whitespace using Trim().
string formInput = "   akiba   ";
Console.WriteLine(formInput.Trim());      // akiba
Console.WriteLine(formInput.TrimStart()); // akiba   (trailing spaces remain)
Console.WriteLine(formInput.TrimEnd());   //    akiba (leading spaces remain)

// A common pattern for normalizing user input.
string rawInput = "  User@Example.COM  ";
string normalized = rawInput.Trim().ToLower();
Console.WriteLine(normalized); // user@example.com

Notes

All of these methods leave the original string unchanged and return a new string. When processing user input from a form, a very common pattern is to call Trim() to remove whitespace and then ToLower() to normalize the case.

Trim() removes not only spaces but also tabs (\t) and newlines (\n). If you need to remove only specific characters, you can use the Trim(char[] trimChars) overload to specify which characters to strip.

For string replacement and containment checks, see Replace() / Contains().

If you find any errors or copyright issues, please .