BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Simple String Formatting Functions

The twenty-first part of the C# Fundamentals tutorial starts an examination of the string manipulation functionality provided by the String class. This article investigates some of the simple but often used string formatting methods.

Trimming

Strings inputted by users can include additional whitespace at the start or the end of the string. Often you will want to remove such excess spaces. The String class provides three methods for doing so. The TrimStart and TrimEnd methods remove whitespace from either the start or the end of a string. The Trim method combines the two functions by removing leading and trailing whitespace with a single call.

string inputted = "  BlackWasp  ";

Console.WriteLine(inputted.TrimStart());        // Outputs "BlackWasp  "
Console.WriteLine(inputted.TrimEnd());          // Outputs "  BlackWasp"
Console.WriteLine(inputted.Trim());             // Outputs "BlackWasp"

It is important to note that the Trim methods return a new string with whitespace removed. The original string is unaffected.

string original = "  BlackWasp  ";
string trimmed = original.Trim();

Console.WriteLine(original);                    // Outputs "  BlackWasp  "
Console.WriteLine(trimmed);                     // Outputs "BlackWasp"

Padding

The String class provides two methods for padding, where spaces are added to the left or right of a string to achieve a desired length. This is useful when producing fixed-width columns of information. The PadLeft method adds spaces to the left of a string and the PadRight add them to the right. An integer argument indicates how long the resultant string should be. If the original string is longer than the parameter value indicates, the resultant string will be the same as the original.

string value1 = "£1.00";
string value2 = "£10.00";
string value3 = "£100.00";

// Output a right-aligned column
Console.WriteLine(value1.PadLeft(8));           // Outputs "   £1.00"
Console.WriteLine(value2.PadLeft(8));           // Outputs "  £10.00"
Console.WriteLine(value3.PadLeft(8));           // Outputs " £100.00"

// Output a left-aligned column
Console.WriteLine(value1.PadRight(8));          // Outputs "£1.00   "
Console.WriteLine(value2.PadRight(8));          // Outputs "£10.00  "
Console.WriteLine(value3.PadRight(8));          // Outputs "£100.00 "

// Trying to pad a string that is too long
Console.WriteLine(value1.PadRight(3));          // Outputs "£1.00"

The padding functions can be called with a second argument. This parameter is a char value holding a character that is used instead of the default whitespace padding character.

string value = "£1.00";

Console.WriteLine(value.PadLeft(8,'.'));        // Outputs "...£1.00"
Console.WriteLine(value.PadRight(8,'_'));       // Outputs "£1.00___"

Text Case Conversion

The last formatting functions to be considered in this article are the case conversion methods. The String class allows strings to be converted to upper or lower case text using the user's local culture settings to ensure correct text conversion for various languages. The methods are named ToLower and ToUpper.

string webSite = "BlackWasp";

Console.WriteLine(webSite.ToLower());           // Outputs "blackwasp"
Console.WriteLine(webSite.ToUpper());           // Outputs "BLACKWASP"
19 November 2006