You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each `{ }` is an *interpolation expression*. C# evaluates the expression, converts the result to a string by calling its `ToString` method, and substitutes the text into the result. If the expression evaluates to`null`, C# substitutes an empty string. Interpolated strings are a more readable alternative to <xref:System.String.Format*?displayProperty=nameWithType>and support the full [composite formatting](../../../standard/base-types/composite-formatting.md) feature set.
20
+
Each `{ }` is an *interpolation expression*. C# evaluates the expression, converts the result to a string by calling its `ToString()` method, and substitutes the text into the result. The string interpolation for a`null` expression is the empty string. In most cases, the default conversion produces the output you want, and you don't need to do anything more.
21
21
22
-
For the language-reference treatment of the syntax and the underlying handler types, see [interpolated string token](../../language-reference/tokens/interpolated.md). For performance-focused topics such as `Span<char>` interpolation and custom interpolated string handlers, see [String operations](../../language-reference/builtin-types/string-operations.md).
22
+
Interpolated strings are a more readable alternative to <xref:System.String.Format*?displayProperty=nameWithType>, and they support the full [composite formatting](../../../standard/base-types/composite-formatting.md) feature set. Anything you can do with a classic positional format string—format specifiers, alignment, culture-aware formatting, and constant strings—you can also do with an interpolated string. The rest of this article covers those options. Reach for them only when you need finer control over the result; otherwise, the plain `$"{expression}"` form is enough.
23
+
24
+
For the language-reference treatment of the syntax and the underlying handler types, see [interpolated string](../../language-reference/tokens/interpolated.md) reference. For performance-focused topics such as `Span<char>` interpolation and custom interpolated string handlers, see [String operations](../../language-reference/builtin-types/string-operations.md).
23
25
24
26
## Apply a format string
25
27
@@ -29,7 +31,7 @@ To control how an expression result is formatted, follow the expression with a c
29
31
{<expression>:<formatString>}
30
32
```
31
33
32
-
The following example formats a date and a numeric value:
34
+
The following example formats a <xref:System.DateTime> and a <xref:System.Double> value:
@@ -63,22 +65,19 @@ For paths and other strings that contain backslashes, prefer an [interpolated ra
63
65
64
66
## Use a conditional expression
65
67
66
-
The colon has special meaning inside an interpolation expression, so wrap a [conditional operator](../../language-reference/operators/conditional-operator.md) in parentheses:
68
+
The colon has special meaning inside an interpolation expression, so wrap a [conditional exoressuin](../../language-reference/operators/conditional-operator.md) in parentheses:
For better readability, break long interpolation expressions across multiple lines. The following example uses an [interpolated raw string literal](../../language-reference/tokens/raw-string.md) so the expression body can wrap freely:
74
+
For better readability, break long interpolation expressions across multiple lines. Multi-line interpolation expressions are available since C# 11. The following example adds newlines between the `{` and `}` characters to separate the interpolated expressions from the literal text:
> Multi-line interpolation expressions are available since C# 11.
78
-
79
78
## Build constant strings
80
79
81
-
When every interpolated expression is itself a constant `string`, the whole interpolated string is also a `const`. That makes it usable for attribute arguments, `switch` patterns, and other contexts that require compile-time constants:
80
+
You can build constant interpolated strings when every interpolated expression is a constant value.That makes it usable for attribute arguments, `switch` patterns, and other contexts that require compile-time constants:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/builtin-types/string-operations.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ ai-usage: ai-assisted
8
8
9
9
# String operations: pattern matching, performance, and span-based search
10
10
11
-
This article covers string operations that go beyond the everyday `Contains`, `IndexOf`, and `Split` methods covered in the Fundamentals [Search strings](../../fundamentals/strings/search.md) and [Split strings into substrings](../../fundamentals/strings/split.md) articles. It focuses on three areas: regular-expression pattern matching, allocation-free search over <xref:System.ReadOnlySpan%601>, and performance considerations for `string` comparison.
11
+
This article covers three string operations: regular-expression pattern matching with <xref:System.Text.RegularExpressions.Regex?displayProperty=nameWithType>, allocation-free search over <xref:System.ReadOnlySpan%601>, and choosing a <xref:System.StringComparison> value for correct, fast comparisons.
12
12
13
13
## Find specific text by using regular expressions
0 commit comments