C# 使用c替换字符串中的值#

C# 使用c替换字符串中的值#,c#,C#,我有一个php示例,正在尝试用c#复制它。最好的方法是什么?我好像找不到我要找的东西 $string = 'The event will take place between :start and :end'; $replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string); 它用数组中的时间填充:start和:end。不需要正则表达式,只需格式化即可 String.Format("The event will

我有一个php示例,正在尝试用c#复制它。最好的方法是什么?我好像找不到我要找的东西

$string = 'The event will take place between :start and :end';

$replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);

它用数组中的时间填充:start和:end。

不需要正则表达式,只需格式化即可

String.Format("The event will take place between {0} and {1}", "8:30", "9:00");

不需要正则表达式,只需格式化即可

String.Format("The event will take place between {0} and {1}", "8:30", "9:00");

您可以使用复合格式字符串插值功能

String.Format
使用复合格式功能:

// Composite formatting:
var string = "The event will take place between {0} and {1}";
var replaced = String.Format(string, "8:30", "9:00");
或者在字符串开头使用
$
,并很容易地将参数传递到其中:

// String interpolation:
var replaced = $"The event will take place between {"8:30"} and {"9:00"}";
字符串插值为创建格式化字符串提供了比字符串复合格式化功能更可读、更方便的语法

有关更多信息,请访问以下链接:


您可以使用组合格式或字符串插值功能

String.Format
使用复合格式功能:

// Composite formatting:
var string = "The event will take place between {0} and {1}";
var replaced = String.Format(string, "8:30", "9:00");
或者在字符串开头使用
$
,并很容易地将参数传递到其中:

// String interpolation:
var replaced = $"The event will take place between {"8:30"} and {"9:00"}";
字符串插值为创建格式化字符串提供了比字符串复合格式化功能更可读、更方便的语法

有关更多信息,请访问以下链接: