Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain how to call string method #2302

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading