Čítať v angličtine Upraviť

Zdieľať cez

Hello World - Introductory interactive tutorial

Search strings

The other part of a search and replace operation is to find text in a string. You can use the Contains method for searching. It tells you if a string contains a substring inside it. Try the following code to explore Contains:

C#
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("greetings"));

The Contains method returns a boolean value which tells you if the string you were searching for was found. A boolean stores either a true or a false value. When displayed as text output, they are capitalized: True and False, respectively. You'll learn more about boolean values in a later lesson.

Challenge

There are two similar methods, StartsWith and EndsWith that also search for sub-strings in a string. These find a substring at the beginning or the end of the string. Try to modify the previous sample to use StartsWith and EndsWith instead of Contains. Search for "You" or "goodbye" at the beginning of a string. Search for "hello" or "goodbye" at the end of a string.

Poznámka

Watch your punctuation when you test for the text at the end of the string. If the string ends with a period, you must check for a string that ends with a period.

You should get true for starting with "You" and ending with "hello" and false for starting with or ending with "goodbye".

Try the code in your browser