字符串。在C#NET中替换
我想知道为什么它不起作用:字符串。在C#NET中替换,c#,.net,replace,C#,.net,Replace,我想知道为什么它不起作用: string filename = optionFileNameFormat; // "{year}-{month}-{day} {name}" Dictionary<string, string> tagList = new Dictionary<string, string>(); tagList.Add("author",System.Security.Principal.WindowsIdenti
string filename = optionFileNameFormat; // "{year}-{month}-{day} {name}"
Dictionary<string, string> tagList = new Dictionary<string, string>();
tagList.Add("author",System.Security.Principal.WindowsIdentity.GetCurrent().Name);
tagList.Add("year" , "" + DateTime.Now.Year);
tagList.Add("month", "" + DateTime.Now.Month);
tagList.Add("day" , "" + DateTime.Now.Day);
foreach (var property in tagList)
{
filename.Replace(@"{" + property.Key + @"}", property.Value);
}
string filename=optionFileNameFormat;//“{year}-{month}-{day}{name}”
字典标记列表=新字典();
tagList.Add(“作者”,System.Security.Principal.WindowsIdentity.GetCurrent().Name);
标记列表。添加(“年”,“日期时间+DateTime.Now.year”);
tagList.Add(“月”,“月+日期时间.Now.month”);
标记列表。添加(“日”,“日+DateTime.Now.day”);
foreach(标记列表中的var属性)
{
文件名.Replace(@“{”+property.Key+@“}”,property.Value);
}
我没有任何错误,但我的字符串没有更改。可能还有其他问题,但我马上想到的是
Replace()
函数没有更改字符串。相反,它返回一个新字符串。因此,您需要将函数的结果分配回原始值:
filename = filename.Replace(@"{" + property.Key + @"}", property.Value);
可能还有其他问题,但我马上想到的是
Replace()
函数不会更改字符串。相反,它返回一个新字符串。因此,您需要将函数的结果分配回原始值:
filename = filename.Replace(@"{" + property.Key + @"}", property.Value);
该方法返回一个新字符串。它不会更改原始字符串
返回一个新字符串,其中包含指定Unicode的所有匹配项
当前字符串中的一个或多个字符将替换为另一个
指定的Unicode字符或字符串
因此,您应该在foreach
循环中分配一个新字符串或一个现有字符串:
filename = filename.Replace(@"{" + property.Key + @"}", property.Value);
或
请记住,在.NET中,字符串是。你不能改变它们。即使您认为您更改了它们,也会创建新的字符串对象。该方法返回一个新字符串。它不会更改原始字符串
返回一个新字符串,其中包含指定Unicode的所有匹配项
当前字符串中的一个或多个字符将替换为另一个
指定的Unicode字符或字符串
因此,您应该在foreach
循环中分配一个新字符串或一个现有字符串:
filename = filename.Replace(@"{" + property.Key + @"}", property.Value);
或
请记住,在.NET中,字符串是。你不能改变它们。即使您认为您更改了它们,也会创建新的字符串对象。中
foreach (var property in tagList)
{
filename.Replace(@"{" + property.Key + @"}", property.Value);
}
只需做以下更改:
filename = filename.Replace(@"{" + property.Key + @"}", property.Value);
在
只需做以下更改:
filename = filename.Replace(@"{" + property.Key + @"}", property.Value);
这是完整的代码:
string filename = optionFileNameFormat; // "{year}-{month}-{day} {name}"
Dictionary<string, string> tagList = new Dictionary<string, string>();
tagList.Add("author",System.Security.Principal.WindowsIdentity.GetCurrent().Name);
tagList.Add("year" , "" + DateTime.Now.Year);
tagList.Add("month", "" + DateTime.Now.Month);
tagList.Add("day" , "" + DateTime.Now.Day);
foreach (var property in tagList)
{
filename= filename.Replace(@"{" + property.Key + @"}", property.Value);
}
string filename=optionFileNameFormat;//“{year}-{month}-{day}{name}”
字典标记列表=新字典();
tagList.Add(“作者”,System.Security.Principal.WindowsIdentity.GetCurrent().Name);
标记列表。添加(“年”,“日期时间+DateTime.Now.year”);
tagList.Add(“月”,“月+日期时间.Now.month”);
标记列表。添加(“日”,“日+DateTime.Now.day”);
foreach(标记列表中的var属性)
{
filename=filename.Replace(@“{”+property.Key+@“}”,property.Value);
}
这是完整的代码:
string filename = optionFileNameFormat; // "{year}-{month}-{day} {name}"
Dictionary<string, string> tagList = new Dictionary<string, string>();
tagList.Add("author",System.Security.Principal.WindowsIdentity.GetCurrent().Name);
tagList.Add("year" , "" + DateTime.Now.Year);
tagList.Add("month", "" + DateTime.Now.Month);
tagList.Add("day" , "" + DateTime.Now.Day);
foreach (var property in tagList)
{
filename= filename.Replace(@"{" + property.Key + @"}", property.Value);
}
string filename=optionFileNameFormat;//“{year}-{month}-{day}{name}”
字典标记列表=新字典();
tagList.Add(“作者”,System.Security.Principal.WindowsIdentity.GetCurrent().Name);
标记列表。添加(“年”,“日期时间+DateTime.Now.year”);
tagList.Add(“月”,“月+日期时间.Now.month”);
标记列表。添加(“日”,“日+DateTime.Now.day”);
foreach(标记列表中的var属性)
{
filename=filename.Replace(@“{”+property.Key+@“}”,property.Value);
}
详细说明:这是因为字符串是不可变的,因此如果调用方法来更改字符串,实际情况是该方法返回更改的字符串,而原始字符串未修改。感谢您的回答详细说明:这是因为字符串是不可变的,所以如果调用方法来更改字符串,实际情况是,该方法返回更改的字符串,而原始字符串未被修改。谢谢您的回答