{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Searching Strings\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=JL30gSE3WaQ&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=4) for this module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contains\n", "\n", "Does your string contain another string within it? You can use `Contains` to find out!\n", "The `Contains` method returns a *boolean*. That's a type represented by the keyword `bool` that can hold two values: `true` or `false`. In this case, the method returns `true` when sought string is found, and `false` when it's not found.\n", "> Run the following code.\n", ">\n", "> What else would or wouldn't be contained?\n", ">\n", "> Does case matter?\n", ">\n", "> Can you store the return value of the `Contains` method> Remember the type of the result is a `bool`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\r\n", "False\r\n" ] } ], "source": [ "string songLyrics = \"You say goodbye, and I say hello\";\n", "Console.WriteLine(songLyrics.Contains(\"goodbye\"));\n", "Console.WriteLine(songLyrics.Contains(\"greetings\"));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## StartsWith and EndsWith\n", "\n", "`StartsWith` and `EndsWith` are methods similar to `Contains`, but more specific. They tell you if a string starts with or ends with the string you're checking. It has the same structure as `Contains`, that is: `bigstring.StartsWith(substring)`\n", "> Now you try!\n", "> In the following code, try searching the line to see if it starts with \"you\" or \"I\".\n", "> Next, see if the code ends with \"hello\" or \"goodbye\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "string songLyrics = \"You say goodbye, and I say hello\";" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playground\n", "\n", "Play around with what you've learned! Here's some starting ideas:\n", "> How many lines say hello?\n", "> Which lines start with \"You\"?\n", "> Which lines end with \"no\"?\n", "> Think back to the previous module. Can you make some lines all uppercase and some lines all lowercase?\n", "> If you change case, how does that affect `Contains`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Playground\r\n" ] } ], "source": [ "Console.WriteLine(\"Playground\");\n", "String line1 = \"You say yes, I say no\";\n", "String line2 = \"You say stop and I say go, go, go\";\n", "String line3 = \"Oh, no\";\n", "String line4 = \"You say goodbye and I say hello\";\n", "String line5 = \"Hello, hello\";\n", "String line6 = \"I don't know why you say goodbye, I say hello\";" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module - Numbers and Integer Math](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/04-Numbers%20and%20Integer%20Math.ipynb)\n", ">\n", "> [⏪ Last Module - The Basics of Strings](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/02-The%20Basics%20of%20Strings.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=JL30gSE3WaQ&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=4)\n", ">\n", "> [Documentation: Intro to C#](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/hello-world?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other resources\n", "\n", "Here's some more places to explore:\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }