Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 替换字符串中文本的最佳方法_C# - Fatal编程技术网

C# 替换字符串中文本的最佳方法

C# 替换字符串中文本的最佳方法,c#,C#,寻找更好的算法/技术来替换字符串变量中的字符串。我必须循环遍历未知数量的数据库记录,对于每个记录,我需要替换字符串变量中的一些文本。现在看起来是这样,但必须有更好的方法: 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&

寻找更好的算法/技术来替换字符串变量中的字符串。我必须循环遍历未知数量的数据库记录,对于每个记录,我需要替换字符串变量中的一些文本。现在看起来是这样,但必须有更好的方法:

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);
    }
}
使用(eds ctx=new eds())
{
string targetText=“这是一个示例字符串,其中的单词将根据从数据库中提取的数据进行替换”;
List lstParameters=ctx.ciParameters.ToList();
foreach(lstParameters中的参数)
{
字符串searchKey=parameter.searchKey;
字符串newValue=parameter.value;
targetText=targetText.Replace(searchKey,newValue);
}
}
据我所知,这并不好,因为我在循环中一次又一次地过度编写targetText变量。但是,我不确定如何查找和替换结构

感谢您的反馈

一定有更好的办法

字符串是不可变的—您不能“更改”它们—您所能做的就是创建一个新字符串并替换变量值(这并不像您想象的那么糟糕)。您可以按照其他人的建议尝试使用
StringBuilder
,但它不能100%保证提高性能

您可以将算法更改为循环遍历
targetText
中的“words”,查看
参数中是否有匹配项,获取“replacement”值并构建一个新字符串,但我怀疑额外的查找将比多次重新创建字符串值花费更多

在任何情况下,都应考虑性能改进的两个重要原则:

  • 首先从应用程序最慢的部分开始——你可能会看到一些改进,但如果它不能显著提高整体性能,那么这也没什么大不了的
  • 要知道某个特定的改变是否会提高你的绩效(以及提高多少),唯一的办法就是从两个方面进行尝试并加以衡量
一定有更好的办法

字符串是不可变的—您不能“更改”它们—您所能做的就是创建一个新字符串并替换变量值(这并不像您想象的那么糟糕)。您可以按照其他人的建议尝试使用
StringBuilder
,但它不能100%保证提高性能

您可以将算法更改为循环遍历
targetText
中的“words”,查看
参数中是否有匹配项,获取“replacement”值并构建一个新字符串,但我怀疑额外的查找将比多次重新创建字符串值花费更多

在任何情况下,都应考虑性能改进的两个重要原则:

  • 首先从应用程序最慢的部分开始——你可能会看到一些改进,但如果它不能显著提高整体性能,那么这也没什么大不了的
  • 要知道某个特定的改变是否会提高你的绩效(以及提高多少),唯一的办法就是从两个方面进行尝试并加以衡量
StringBuilder将具有更少的内存开销和更好的性能,尤其是在大型字符串上

使用(eds ctx=new eds())
{
string targetText=“这是一个示例字符串,其中的单词将根据从数据库中提取的数据进行替换”;
var builder=新的StringBuilder(targetText);
List lstParameters=ctx.ciParameters.ToList();
foreach(lstParameters中的参数)
{
字符串searchKey=parameter.searchKey;
字符串newValue=parameter.value;
targetText=builder.Replace(searchKey,newValue);
}
}

StringBuilder将具有更少的内存开销和更好的性能,尤其是在大型字符串上

使用(eds ctx=new eds())
{
string targetText=“这是一个示例字符串,其中的单词将根据从数据库中提取的数据进行替换”;
var builder=新的StringBuilder(targetText);
List lstParameters=ctx.ciParameters.ToList();
foreach(lstParameters中的参数)
{
字符串searchKey=parameter.searchKey;
字符串newValue=parameter.value;
targetText=builder.Replace(searchKey,newValue);
}
}
事实上,假设您正在进行大量替换,有一个更好的答案。您可以使用
StringBuilder
。正如您所知,字符串是不可变的。正如您所说,您在循环中一次又一次地创建字符串

如果将字符串转换为
StringBuilder

StringBuilder s = new StringBuilder(s, s.Length*2); // Adjust the capacity based on how much bigger you think the string will get due to replacements. The more accurate your estimate, the better this will perform.

  foreach (parameter in lstParameters)
    {
        s.Replace(parameter.searchKey, parameter.value);
    }
  string targetString = s.ToString();
现在需要注意的是,如果您的列表中只有2-3项,这可能不会更好。对的答案提供了一个很好的分析,说明了您可以期望看到的性能改进。

事实上,假设您正在进行大量替换,则有一个更好的答案。您可以使用
StringBuilder
。正如您所知,字符串是不可变的。正如您所说,您在循环中一次又一次地创建字符串

如果将字符串转换为
StringBuilder

StringBuilder s = new StringBuilder(s, s.Length*2); // Adjust the capacity based on how much bigger you think the string will get due to replacements. The more accurate your estimate, the better this will perform.

  foreach (parameter in lstParameters)
    {
        s.Replace(parameter.searchKey, parameter.value);
    }
  string targetString = s.ToString();

现在需要注意的是,如果您的列表中只有2-3项,这可能不会更好。对的回答提供了一个很好的分析,说明了您可以期望看到的性能改进。

您关心的是性能,或者某个被替换的值可能再次被替换(
“asd”)。替换(“a”、“s”)。替换(“s”、“d”)
)?您关心的是性能或某个被替换的值可能再次被替换的事实(
“asd”).replace(“a”、“s”).replace(“s”、“d”)
)?为什么在某些情况下StringBuilder.replace()不是一个好的替代品?它是可变的,开销可能并不总是一个问题。如果我不高兴,请纠正我wrong@MarcWittmann可能是的,我会修改我的答案,使其不那么绝对。为什么在某些情况下,StringBuilder.Replace()不是一个好的选择?它是可变的,开销可能并不总是一个问题。如果我不高兴,请纠正我wrong@MarcWittmann我