C# 0 The Complete Reference



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

StringSplitOptions

enumeration defines only two values: 



None

 and


 RemoveEmptyEntries

. If 


how

 is 


None

,

then empty strings are included in the result (as the previous program showed). If 



how

 is 


RemoveEmptyEntries

, empty strings are excluded from the result.

To understand the effects of removing empty entries, try replacing this line in the 

preceding program:

string[] parts = str.Split(seps);

with the following:

string[] parts = str.Split(seps, StringSplitOptions.RemoveEmptyEntries);

When you run the program, the output will be as shown next:

Pieces from split:

One


if

by

land



two

if

by



sea

Result of join:

One | if | by | land | two | if | by | sea

As you can see, the empty string that previously resulted because of the combination of the 

comma and space after “land” has been removed.

Splitting a string is an important string-manipulation procedure because it is often used 

to obtain the individual 

tokens

 that comprise the string. For example, a database program 

might use 

Split( )

 to decompose a query such as “show me all balances greater than 100” 

www.freepdf-books.com



680

 

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

into its individual parts, such as “show” and “100”. In the process, the separators are 

removed. Thus, “show” (without any leading or trailing spaces) is obtained, not “ show”. The 

following program illustrates this concept. It tokenizes strings containing binary mathematical 

operations, such as 10 + 5. It then performs the operation and displays the result.

// Tokenize strings.

using System;

class TokenizeDemo {

  static void Main() {

    string[] input = {

                      "100 + 19",

                      "100 / 3.3",

                      "-3 * 9",

                      "100 - 87"

                     };

    char[] seps = {' '};

    for(int i=0; i < input.Length; i++) {

      // split string into parts

      string[] parts = input[i].Split(seps);

      Console.Write("Command: ");

      for(int j=0; j < parts.Length; j++)

        Console.Write(parts[j] + " ");

      Console.Write(", Result: ");

      double n = Double.Parse(parts[0]);

      double n2 = Double.Parse(parts[2]);

      switch(parts[1]) {

        case "+":

          Console.WriteLine(n + n2);

          break;

        case "-":

          Console.WriteLine(n - n2);

          break;

        case "*":

          Console.WriteLine(n * n2);

          break;

        case "/":

          Console.WriteLine(n / n2);

          break;

      }

    }


  }

}

Here is the output:



Command: 100 + 19 , Result: 119

Command: 100 / 3.3 , Result: 30.3030303030303

Command: -3 * 9 , Result: -27

Command: 100 - 87 , Result: 13

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   838   839   840   841   842   843   844   845   ...   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