{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The Basics of Strings\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=JSpC7Cz64h0&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=3) for this module.\n", "\n", "## What is a String?\n", "\n", "A string is a sequence of characters. A handy metaphor is a friendship bracelet, where you string together letters to make a name." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings and String Literals\n", "\n", "`firstFriend` and `secondFriend` are variables of strings. The line within `Console.WriteLine` is also a string. It's a **string literal**. A string literal is text what represents a constant string.\n", "\n", "> Try that out with the following code. Press play and see what comes out.\n", ">\n", "> Next, try changing the variables to see different names." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My friends are Maria and Sophia\r\n" ] } ], "source": [ "string firstFriend = \"Maria\";\n", "string secondFriend = \"Sophia\";\n", "Console.WriteLine($\"My friends are {firstFriend} and {secondFriend}\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## String Properties\n", "\n", "As you explore more with strings, you'll find that strings are more than a collection of letters. You can find the length of a string using `Length`. `Length` is a **property** of a string and it returns the number of characters in that string.\n", "\n", "> Try that out by seeing how long the names of the friends are:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The name Maria has 5 letters.\r\n", "The name Sophia has 6 letters.\r\n" ] } ], "source": [ "Console.WriteLine($\"The name {firstFriend} has {firstFriend.Length} letters.\");\n", "Console.WriteLine($\"The name {secondFriend} has {secondFriend.Length} letters.\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# String Methods\n", "\n", "## Leading and Trailing Spaces\n", "\n", "Suppose your strings have leading or trailing spaces (also called **white space**) that you don't want to display. You want to trim the spaces from the strings. The `Trim` method and related methods `TrimStart` and `TrimEnd` do that work. You can just use those methods to remove leading and trailing spaces.\n", "\n", "> Play around with trimming in the following code. The brackets are there to help you see all the white space.\n", "\n", "**Editing Note: Should this be all one thing? Or multiple breakups of code and markdown?*" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ Hello World! ]\r\n", "[Hello World! ]\r\n", "[ Hello World!]\r\n", "[Hello World!]\r\n" ] } ], "source": [ "string greeting = \" Hello World! \";\n", "Console.WriteLine($\"[{greeting}]\");\n", "\n", "string trimmedGreeting = greeting.TrimStart();\n", "Console.WriteLine($\"[{trimmedGreeting}]\");\n", "\n", "trimmedGreeting = greeting.TrimEnd();\n", "Console.WriteLine($\"[{trimmedGreeting}]\");\n", "\n", "trimmedGreeting = greeting.Trim();\n", "Console.WriteLine($\"[{trimmedGreeting}]\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Replace\n", "\n", "You can also replace substrings with other values. For example, in the code below, you can take \"Hello World!\" and replace \"Hello\" with \"Greetings\", to make \"Greetings World!\"\n", "\n", "> Try it out. What else could you replace \"Hello\" with?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\r\n", "Greetings World!\r\n" ] } ], "source": [ "string sayHello = \"Hello World!\";\n", "Console.WriteLine(sayHello);\n", "sayHello = sayHello.Replace(\"Hello\", \"Greetings\");\n", "Console.WriteLine(sayHello);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing Case\n", "\n", "Sometimes you need your strings to be all UPPERCASE or all lowercase. `ToUpper` and `ToLower` do just that.\n", "> The following example seems a bit mixed up. Can you fix it so \"whisper\" is all lowercase, and \"shout\" is all uppercase?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WHISPER\r\n", "shout\r\n" ] } ], "source": [ "Console.WriteLine(\"WhiSPer\".ToUpper());\n", "Console.WriteLine(\"sHoUt\".ToLower());" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playground\n", "\n", "Now it's your turn to play around with what you've learned. Try these exercises:\n", "> Create three variables of three different people.\n", ">\n", "> Find the length of the first person, make the second person all caps, and the third person all lowercase.\n", ">\n", "> How many letters are in \"supercalifragilisticexpialidocious\"?\n", ">\n", "> How many characters are taken out when you trim \" friendship bracelet \"? Does trimming take out the center space?\n", ">\n", "> What do you want to write?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Playground\r\n", "supercalifragilisticexpialidocious\r\n", " friendship bracelet \r\n" ] } ], "source": [ "Console.WriteLine(\"Playground\");\n", "Console.WriteLine(\"supercalifragilisticexpialidocious\");\n", "Console.WriteLine(\" friendship bracelet \");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module - Searching Strings](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/03-Searching%20Strings.ipynb)\n", ">\n", "> [⏪ Last Module - Hello World](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/01-Hello%20World.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=JSpC7Cz64h0&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=3)\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 }