BlackWasp
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

Often a string that has been input by the user will include additional whitespace characters at the start or the end of the string. Sometimes the user may enter only whitespace where information is required and this must be tested for. In both cases, there is a need to remove excess whitespace characters from the string.

The String class provides three methods for removing whitespace. 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 not affected by the operation.

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. This is almost the reverse action to trimming in that it adds spaces to the left or right of a string until the resultant string is of a specified length. This is useful when producing fixed-width columns of information where each column needs to be aligned to the left or right. The PadLeft method adds spaces to the left of a string and the PadRight add them to the right. An integer parameter is used to indicate how long the resultant string should be. If the original string is longer than the parameter value indicates then the resultant string will be equal to the original string.

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 also be called with a second parameter. This parameter is a char value holding a single 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 provides methods to allow a string to be converted to upper case or lower case text. These use the current local culture settings of the execution environment 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"
Link to this Page19 November 2006
RSS RSS Feed