【发布时间】:2015-10-09 19:02:36
【问题描述】:
寻找更好的算法/技术来替换字符串变量中的字符串。我必须遍历未知数量的数据库记录,并且对于每一个,我都需要替换字符串变量中的一些文本。现在看起来像这样,但必须有更好的方法:
using (eds ctx = new eds())
{
string targetText = "This is a sample string with words that will get replaced based on data pulled from the database";
List<parameter> lstParameters = ctx.ciParameters.ToList();
foreach (parameter in lstParameters)
{
string searchKey = parameter.searchKey;
string newValue = parameter.value;
targetText = targetText.Replace(searchKey, newValue);
}
}
据我了解,这并不好,因为我在循环中一遍又一遍地编写了 targetText 变量。但是,我不确定查找和替换的结构如何......
感谢任何反馈。
【问题讨论】:
-
您担心性能或某些替换值可能再次被替换 (
"asd".Replace("a", "s").Replace("s", "d"))?。
标签: c#