Skip to content

Commit

Permalink
Explain how to call string method (#2302)
Browse files Browse the repository at this point in the history
  • Loading branch information
iHiD committed Sep 4, 2024
1 parent e44a959 commit 12839e1
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion exercises/concept/log-levels/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -18,10 +32,15 @@ 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
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

0 comments on commit 12839e1

Please sign in to comment.