freeCodeCamp/guide/chinese/csharp/string-interpolation/index.md

29 lines
1.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: String Interpolation
localeTitle: 字符串插值
---
# 字符串插值
在C通常要连接字符串您可以使用“+”运算符或复合格式与String.Format之类的方法。通过复合格式化我指的是带有索引占位符格式项的格式字符串以及要在占位符中使用的对象列表。
#
```
string message = "Hello " + firstName + " " + lastName + "!";
string message2 = string.Format("Hello {0} {1}!", firstName, lastName);
```
对于插值字符串表达式,您有一个包含表达式的字符串,该表达式将替换为表达式的结果。您必须在字符串文字前面加上美元符号($)。您希望包含在字符串中的表达式以花括号括起来。上面的消息现在看起来像这样:
#
```
string message = $"Hello {firstName} {lastName}!";
```
**一点点有用的信息** 在字符串插值中,您可以调用函数,属性和三元运算符:
```
int a = 3;
int b = 454;
string result = $"{a}+{b} = {a+b}";
```