PART II
C h a p t e r 2 2 :
S t r i n g s a n d F o r m a t t i n g
681
Padding and Trimming Strings
Sometimes you will want to remove leading and trailing spaces from a string. This type of
operation, called
trimming,
is often needed by command processors. For example, a database
might recognize the word “print.” However, a user might enter this command with one or
more leading or trailing spaces. Any such spaces must be removed before the string can be
recognized by the database. Conversely, sometimes you will want to pad a string with
spaces so that it meets some minimal length. For example, if you are preparing formatted
output, you might need to ensure that each line is of a certain length in order to maintain
alignment. Fortunately, C# includes methods that make these types of operations easy.
To trim a string, use one of these
Trim( )
methods:
public string Trim( )
public string Trim(params char[ ]
chrs
)
The first form removes leading and trailing whitespace from the invoking string. The second
form removes leading and trailing occurrences of the characters specified by
chrs.
In both
cases, the resulting string is returned.
You can pad a string by adding characters to either the left or the right side of the string.
To pad a string on the left, use one of the methods shown here:
public string PadLeft(int
len
)
public string PadLeft(int
len
, char
ch
)
The first form adds spaces on the left as needed to the invoking string so that its total length
equals
len.
The second form adds the character specified by
ch
as needed to the invoking
string so that its total length equals
len.
In both cases, the resulting string is returned. If
len
is
less than the length of the invoking string, a copy of the invoking string is returned unaltered.
To pad a string to the right, use one of these methods:
public string PadRight(int
len
)
public string PadRight(int
len
, char
ch
)
The first form adds spaces on the right as needed to the invoking string so that its total
length equals
len.
The second form adds the characters specified by
ch
as needed to the
invoking string so that its total length equals
len.
In both cases, the resulting string is returned.
If
len
is less than the length of the invoking string, a copy of the invoking string is returned
unaltered.
The following program demonstrates trimming and padding:
// Trimming and padding.
using System;
class TrimPadDemo {
static void Main() {
string str = "test";
Console.WriteLine("Original string: " + str);
// Pad on left with spaces.
str = str.PadLeft(10);
Console.WriteLine("|" + str + "|");
www.freepdf-books.com
682
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
// Pad on right with spaces.
str = str.PadRight(20);
Console.WriteLine("|" + str + "|");
// Trim spaces.
str = str.Trim();
Console.WriteLine("|" + str + "|");
// Pad on left with #s.
str = str.PadLeft(10, '#');
Console.WriteLine("|" + str + "|");
// Pad on right with #s.
str = str.PadRight(20, '#');
Console.WriteLine("|" + str + "|");
// Trim #s.
str = str.Trim('#');
Console.WriteLine("|" + str + "|");
}
}
The output is shown here:
Original string: test
| test|
| test |
|test|
|######test|
|######test##########|
|test|
Do'stlaringiz bilan baham: |