C# c语言中的格式化函数#

C# c语言中的格式化函数#,c#,C#,我有一个格式字符串formatStr和DateTime字符串Dt。我需要用Dt中相应的值替换formatStr中的模式 下面是函数的模板- string stringFormat(formatStr, Dt) %Year has to replace with year %Month has to replace with month . . . and so on 注意:formatStr是常量,Dt是Datetime类型 比如说 stringFormat(@"D:\my\POC\%Yea

我有一个格式字符串formatStr和DateTime字符串Dt。我需要用Dt中相应的值替换formatStr中的模式

下面是函数的模板-

string stringFormat(formatStr, Dt)

%Year has to replace with year
%Month has to replace with month
.
.
.
and so on
注意:formatStr是常量,Dt是Datetime类型

比如说

stringFormat(@"D:\my\POC\%Year\%Month\%Day\File_%Year_%Month_%Day_%Hour_%Minute_%Second_%Milisecond.ss", "2015-03-29 23:55:01.293")
应返回-“D:\my\POC\2015\03\29\File\u 2015\u 03\u 29\u 23\u 55\u 01\u 293.ss”


我不介意换衣服

您可以将格式更改为:

String.Format(@"D:\my\POC\{0:yyyy}\{0:MM}\{0:dd}\File_{0:yyyy}_{0:MM}_{0:dd}_{0:hh}_{0:mm}_{0:ss}_{0:fff}.ss", DateTime.Now)

也可以对每个参数使用字符串替换函数。甚至是正则表达式。但我会选择第1个解决方案,如果它已经返回该值,那么它有什么问题?您应该显示方法
stringFormat
“2015-03-29 23:55:01.293”
也是一个
字符串
而不是
日期时间
字符串。Format()
(恰当命名)可能是您想要的,而不是您自己的方法-要使用string.Format(),我需要多次传递Dt参数来分离所有属性。有没有可以直接使用的内置方法?这里的问题是什么?最后一部分是你想要的或者你已经拥有的东西的最后一部分是你想要的或者你已经拥有的最后一部分吗?@亨霍霍霍勒曼我有Dt(2015-03-03-29 29 29:23最后一部分是你想要的最后一部分是你想要的或者你想要的最后一部分是你想要的或者你已经拥有的最后一部分是你想要的最后一部分是你想要的或者你已经有你已经拥有的东西?@霍霍霍霍霍霍霍霍霍霍霍霍曼我有我有Dt(2015-3-3-3-3-3-3-3-3-3-29-29-29-29-3-29-29-29-3-3-3-3-3-3-3:29-3:29:55:29:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:55:1.1.1.{0:fff}.ss“,Convert.ToDateTime(Dt))因为Dt是以字符串的形式出现的。谢谢