0
点赞
收藏
分享

微信扫一扫

C# $ 特殊字符将字符串文本标识为内插字符串

mafa1993 2022-03-11 阅读 55

$ 特殊字符将字符串文本标识为内插字符串。 内插字符串是可能包含内插表达式的字符串文本 。 将内插字符串解析为结果字符串时,带有内插表达式的项会替换为表达式结果的字符串表示形式。 从 C# 6 开始可以使用此功能。

字符串内插为格式化字符串提供了一种可读性和便捷性更高的方式。 它比字符串复合格式设置更容易阅读。 比较一下下面的示例,它使用了这两种功能产生相同的输出:

C#复制运行

string name = "Mark";
var date = DateTime.Now;

// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Both calls produce the same output that is similar to:
// Hello, Mark! Today is Wednesday, it's 19:40 now.

内插字符串的结构

若要将字符串标识为内插字符串,可在该字符串前面加上 $ 符号。 字符串字面量开头的 $ 和 " 之间不能有任何空格。

具备内插表达式的项的结构如下所示:

C#复制

{<interpolationExpression>[,<alignment>][:<formatString>]}

括号中的元素是可选的。 下表说明了每个元素:

内插字符串的结构
元素描述
interpolationExpression生成需要设置格式的结果的表达式。 null 的字符串表示形式为 String.Empty。
alignment常数表达式,它的值定义表达式结果的字符串表示形式中的最小字符数。 如果值为正,则字符串表示形式为右对齐;如果值为负,则为左对齐。 有关详细信息,请参阅对齐组件。
formatString受表达式结果类型支持的格式字符串。 有关更多信息,请参阅格式字符串组件。

以下示例使用上述可选的格式设置组件:

C#复制运行

Console.WriteLine($"|{"Left",-7}|{"Right",7}|");

const int FieldWidthRightAligned = 20;
Console.WriteLine($"{Math.PI,FieldWidthRightAligned} - default formatting of the pi number");
Console.WriteLine($"{Math.PI,FieldWidthRightAligned:F3} - display only three decimal digits of the pi number");
// Expected output is:
// |Left   |  Right|
//     3.14159265358979 - default formatting of the pi number
//                3.142 - display only three decimal digits of the pi number

从 C# 10 开始,可以使用字符串内插来初始化常量字符串。 用于占位符的所有表达式都必须是常量字符串。 换言之,每个内插表达式都必须是一个字符串,并且必须是编译时常量。

特殊字符

要在内插字符串生成的文本中包含大括号 "{" 或 "}",请使用两个大括号,即 "{{" 或 "}}"。 有关详细信息,请参阅转义大括号。

因为冒号(“:”)在内插表达式项中具有特殊含义,为了在内插表达式中使用条件运算符,请将表达式放在括号内。

以下示例演示了如何在结果字符串中包括大括号。 它还演示了如何使用条件运算符:

C#复制运行

string name = "Horace";
int age = 34;
Console.WriteLine($"He asked, \"Is your name {name}?\", but didn't wait for a reply :-{{");
Console.WriteLine($"{name} is {age} year{(age == 1 ? "" : "s")} old.");
// Expected output is:
// He asked, "Is your name Horace?", but didn't wait for a reply :-{
// Horace is 34 years old.

内插逐字字符串以 $ 字符开头,后跟 @ 字符。 有关逐字字符串的详细信息,请参阅字符串和逐字标识符文章。

 备注

从 C# 8.0 开始,可以按任意顺序使用 $ 和 @ 标记:$@"..." 和 @$"..." 均为有效的内插逐字字符串。 在早期 C# 版本中,$ 标记必须出现在 @ 标记之前。

隐式转换和指定 IFormatProvider 实现的方式

内插字符串有 3 种隐式转换:

  1. 将内插字符串转换为 String 实例。 字符串是内插字符串解析的结果。 所有内插表达式项都被替换为结果的格式设置正确的字符串表示形式。 此转换使用 CurrentCulture 设置表达式结果的格式。

  2. 将内插字符串转换为表示复合格式字符串的 FormattableString 实例,同时也将表达式结果格式化。 这允许通过单个 FormattableString 实例创建多个包含区域性特定内容的结果字符串。 要执行此操作,请调用以下方法之一:

    • ToString() 重载,生成 CurrentCulture 的结果字符串。
    • Invariant 方法,生成 InvariantCulture 的结果字符串。
    • ToString(IFormatProvider) 方法,生成特定区域性的结果字符串。

    ToString(IFormatProvider) 方法提供支持自定义格式设置的 IFormatProvider 接口的用户定义实现。 有关详细信息,请参阅在 .NET 中设置类型格式一文中的使用 ICustomFormatter 进行自定义格式设置部分。

  3. 将内插字符串转换为 IFormattable 实例,使用此实例也可通过单个 IFormattable 实例创建多个包含区域性特定内容的结果字符串。

以下示例通过隐式转换为 FormattableString 来创建特定于区域性的结果字符串:

C#复制运行

double speedOfLight = 299792.458;
FormattableString message = $"The speed of light is {speedOfLight:N3} km/s.";

System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("nl-NL");
string messageInCurrentCulture = message.ToString();

var specificCulture = System.Globalization.CultureInfo.GetCultureInfo("en-IN");
string messageInSpecificCulture = message.ToString(specificCulture);

string messageInInvariantCulture = FormattableString.Invariant(message);

Console.WriteLine($"{System.Globalization.CultureInfo.CurrentCulture,-10} {messageInCurrentCulture}");
Console.WriteLine($"{specificCulture,-10} {messageInSpecificCulture}");
Console.WriteLine($"{"Invariant",-10} {messageInInvariantCulture}");
// Expected output is:
// nl-NL      The speed of light is 299.792,458 km/s.
// en-IN      The speed of light is 2,99,792.458 km/s.
// Invariant  The speed of light is 299,792.458 km/s.

其他资源

如果你不熟悉字符串内插,请参阅 C# 中的字符串内插交互式教程。 还可查看另一个 C# 中的字符串内插教程。 该教程演示了如何使用内插字符串生成带格式的字符串。

内插字符串编译

如果内插字符串类型为 string,则通常将其转换为 String.Format 方法调用。 如果分析的行为等同于串联,则编译器可将 String.Format 替换为 String.Concat。

如果内插字符串类型为 IFormattable 或 FormattableString,则编译器会生成对 FormattableStringFactory.Create 方法的调用。

从 C# 10 开始,使用内插字符串时,编译器将检查内插字符串是否被分配给满足内插字符串处理程序模式要求的类型。 内插字符串处理程序是一种自定义类型,可将内插字符串转换为字符串。 内插字符串处理程序是一种高级方案,通常出于性能原因使用。 可以在内插字符串改进的语言规范中了解生成内插字符串处理程序的要求。 可以按照“C# 新增功能”部分中的内插字符串处理程序教程生成一个内插字符串处理程序。 在 .NET 6 中,当对 string 类型的参数使用内插字符串时,内插字符串由 System.Runtime.CompilerServices.DefaultInterpolatedStringHandler 处理。

 备注

内插字符串处理程序的一个副作用是,自定义处理程序(包括 System.Runtime.CompilerServices.DefaultInterpolatedStringHandler)可能不会在所有条件下都计算内插字符串中用作占位符的所有表达式。 这意味着这些表达式中的副作用可能不会发生。

C# 语言规范

有关详细信息,请参阅 C# 语言规范的内插字符串部分。

举报

相关推荐

0 条评论