C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet835/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   831   832   833   834   835   836   837   838   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

StartsWith( )

 returns true if the invoking string begins with the string passed in 

str.

EndsWith( )

 returns true if the invoking string ends with the string passed in 



str.

 Both 


return false on failure.

Here is a program that demonstrates several of the string search methods:

// Search strings.

using System;

class StringSearchDemo {

  static void Main() {

    string str = "C# has powerful string handling.";

    int idx;

    Console.WriteLine("str: " + str);

    idx = str.IndexOf('h');

    Console.WriteLine("Index of first 'h': " + idx);

www.freepdf-books.com




674

 

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

    idx = str.LastIndexOf('h');

    Console.WriteLine("Index of last 'h': " + idx);

    idx = str.IndexOf("ing");

    Console.WriteLine("Index of first \"ing\": " + idx);

    idx = str.LastIndexOf("ing");

    Console.WriteLine("Index of last \"ing\": " + idx);

    char[] chrs = { 'a', 'b', 'c' };

    idx = str.IndexOfAny(chrs);

    Console.WriteLine("Index of first 'a', 'b', or 'c': " + idx);

    if(str.StartsWith("C# has"))

      Console.WriteLine("str begins with \"C# has\"");

    if(str.EndsWith("ling."))

      Console.WriteLine("str ends with \"ling.\"");

  }

}

The output from the program is shown here:



str: C# has powerful string handling.

Index of first 'h': 3

Index of last 'h': 23

Index of first "ing": 19

Index of last "ing": 28

Index of first 'a', 'b', or 'c': 4

str begins with "C# has"

str ends with "ling."

A string search method that you will find useful in many circumstances is 

Contains( )

.

Its general form is shown here:



public bool Contains(string 

str

)

It returns true if the invoking string contains the string specified by 



str

 and false otherwise. 

This method is especially useful when all you need to know is if a specific substring exists 

within another string. Here is an example that demonstrates its use:

// Demonstrate Contains().

using System;

class ContainsDemo {

  static void Main() {

    string str = "C# combines power with performance.";

    if(str.Contains("power"))

      Console.WriteLine("The sequence power was found.");

    if(str.Contains("pow"))

      Console.WriteLine("The sequence pow was found.");

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   831   832   833   834   835   836   837   838   ...   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