C# 0 The Complete Reference


PART II C h a p t e r   2 2



Download 4,07 Mb.
Pdf ko'rish
bet843/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   839   840   841   842   843   844   845   846   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


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|


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   839   840   841   842   843   844   845   846   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish