From 09c1d58a29b0c45596321724f77b53706fd0ce5e Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Tue, 3 Sep 2024 22:55:09 +0100 Subject: [PATCH] Explain how to call string method --- .../concept/log-levels/.docs/introduction.md | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/exercises/concept/log-levels/.docs/introduction.md b/exercises/concept/log-levels/.docs/introduction.md index 0dad04e59b..2fbe2840c9 100644 --- a/exercises/concept/log-levels/.docs/introduction.md +++ b/exercises/concept/log-levels/.docs/introduction.md @@ -8,7 +8,21 @@ A `string` in C# is an object that represents immutable text as a sequence of Un string fruit = "Apple"; ``` -Strings are manipulated by calling the string's methods. Once a string has been constructed, its value can never change. Any methods that appear to modify a string will actually return a new string. +### Methods + +Strings are manipulated by calling the string's [built-in methods][docs-string-methods]. +For example, to remove the whitespace from a string, you can use the [Trim method][tutorial-docs.microsoft.com-trim-white-space] like this: + +```csharp +string name = " Jane " +name.Trim() +// => "Jane" +``` + +Once a string has been constructed, its value can never change. +Any methods that appear to modify a string will actually return a new string. + +### Concatenation Multiple strings can be concatenated (added) together. The simplest way to achieve this is by using the `+` operator. @@ -18,6 +32,8 @@ string name = "Jane"; // => "Hello Jane!" ``` +### Formatting + For any string formatting more complex than simple concatenation, string interpolation is preferred. To use interpolation in a string, prefix it with the dollar (`$`) symbol. Then you can use curly braces (`{}`) to access any variables inside your string. ```csharp @@ -25,3 +41,6 @@ string name = "Jane"; $"Hello {name}!"; // => "Hello Jane!" ``` + +[docs-string-methods]: https://docs.microsoft.com/en-us/dotnet/api/system.string +[tutorial-docs.microsoft.com-trim-white-space]: https://docs.microsoft.com/en-us/dotnet/csharp/how-to/modify-string-contents#trim-white-space