C# 高效地替换字符串数组中的字符串
我有一个充满guid的字符串数组。我试图用不同的guid替换某些guid。我的方法如下C# 高效地替换字符串数组中的字符串,c#,linq,C#,Linq,我有一个充满guid的字符串数组。我试图用不同的guid替换某些guid。我的方法如下 var newArray = this.to.Select(s => s.Replace("e77f75b7-2373-dc11-8f13-0019bb2ca0a0", "1fe8f3f6-fe17-e811-80d8-00155d5ce473") .Replace("fbd0c892-2373-dc11-8f13-0019bb2ca0a0", "1fe8f3f6-fe17-e811-80d8-
var newArray = this.to.Select(s => s.Replace("e77f75b7-2373-dc11-8f13-0019bb2ca0a0", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
.Replace("fbd0c892-2373-dc11-8f13-0019bb2ca0a0", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
.Replace("76cd4297-1e31-dc11-95d8-0019bb2ca0a0", "eb892fb0-fe17-e811-80d8-00155d5ce473")
.Replace("cd42bb68-2073-dc11-8f13-0019bb2ca0a0", "dc6077e2-fe17-e811-80d8-00155d5ce473")
.Replace("96b97150-cd45-e111-a3d5-00155d10010f", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
).ToArray();
我有几个字段正在执行此操作,这导致了OutOfMemoryException。是不是因为Replace()方法每次都在创建一个新数组?有没有更有效的方法来处理字符串数组?这个方法运行了成千上万条记录,所以我认为这就是问题所在。当我注释掉这些行时,我没有得到异常
编辑:“to”变量中的数据在每种情况下都是一个短字符串,但这会运行数千条记录。所以对于一张唱片来说,“to”可能是这样的
"systemuser|76cd4297-1e31-dc11-95d8-0019bb2ca0a0;contact|96b97150-cd45-e111-a3d5-00155d10010f"
它可能有我想要替换的任何guid,因此,即使该记录中可能只有一个guid,我也需要运行完整的replaces()集合,以防其中有任何guid
任何指针都会很棒!谢谢。我会使用正则表达式提取字段,然后使用替换字典应用更改,然后重新生成字符串,这样做只需一次扫描:
IDictionary<string, string> replacements = new Dictionary<string, string>
{
{"76cd4297-1e31-dc11-95d8-0019bb2ca0a0","something else"},
//etc
};
var newData = data
//.AsParallel() //for speed
.Select(d => Regex.Match(d, @"^(?<f1>[^\|]*)\|(?<f2>[^;]*);(?<f3>[^\|]*)\|(?<f4>.*)$"))
.Where(m => m.Success)
.Select(m => new
{
field1 = m.Groups["f1"].Value,
field2 = m.Groups["f2"].Value,
field3 = m.Groups["f3"].Value,
field4 = m.Groups["f4"].Value
})
.Select(x => new
{
x.field1,
field2 = replacements.TryGetValue(x.field2, out string r2) ? r2 : x.field2,
x.field3,
field4 = replacements.TryGetValue(x.field4, out string r4) ? r4 : x.field4
})
.Select(x => $"{x.field1}|{x.field2};{x.field3}|{x.field4}")
.ToArray();
IDictionary replacements=新词典
{
{“76cd4297-1e31-dc11-95d8-0019BB2CA0”,“其他东西”},
//等
};
var newData=data
//.AsParallel()//表示速度
.Select(d=>Regex.Match(d,@“^(?[^\\\\\\]*)\\\\\\(?[^;]*);(?[^\\\\\\\]*)\\\\\\.$”)
.其中(m=>m.Success)
.选择(m=>new
{
field1=m.Groups[“f1”]。值,
field2=m.Groups[“f2”]值,
field3=m.Groups[“f3”]值,
字段4=m.Groups[“f4”]值
})
.选择(x=>new
{
x、 字段1,
field2=替换.TryGetValue(x.field2,输出字符串r2)?r2:x.field2,
x、 字段3,
field4=替换.TryGetValue(x.field4,输出字符串r4)?r4:x.field4
})
.Select(x=>$“{x.field1}{x.field2};{x.field3}{x.field4}”)
.ToArray();
我会使用正则表达式提取字段,然后使用替换字典应用更改,然后重新组合字符串,在一次扫描中完成此操作:
IDictionary<string, string> replacements = new Dictionary<string, string>
{
{"76cd4297-1e31-dc11-95d8-0019bb2ca0a0","something else"},
//etc
};
var newData = data
//.AsParallel() //for speed
.Select(d => Regex.Match(d, @"^(?<f1>[^\|]*)\|(?<f2>[^;]*);(?<f3>[^\|]*)\|(?<f4>.*)$"))
.Where(m => m.Success)
.Select(m => new
{
field1 = m.Groups["f1"].Value,
field2 = m.Groups["f2"].Value,
field3 = m.Groups["f3"].Value,
field4 = m.Groups["f4"].Value
})
.Select(x => new
{
x.field1,
field2 = replacements.TryGetValue(x.field2, out string r2) ? r2 : x.field2,
x.field3,
field4 = replacements.TryGetValue(x.field4, out string r4) ? r4 : x.field4
})
.Select(x => $"{x.field1}|{x.field2};{x.field3}|{x.field4}")
.ToArray();
IDictionary replacements=新词典
{
{“76cd4297-1e31-dc11-95d8-0019BB2CA0”,“其他东西”},
//等
};
var newData=data
//.AsParallel()//表示速度
.Select(d=>Regex.Match(d,@“^(?[^\\\\\\]*)\\\\\\(?[^;]*);(?[^\\\\\\\]*)\\\\\\.$”)
.其中(m=>m.Success)
.选择(m=>new
{
field1=m.Groups[“f1”]。值,
field2=m.Groups[“f2”]值,
field3=m.Groups[“f3”]值,
字段4=m.Groups[“f4”]值
})
.选择(x=>new
{
x、 字段1,
field2=替换.TryGetValue(x.field2,输出字符串r2)?r2:x.field2,
x、 字段3,
field4=替换.TryGetValue(x.field4,输出字符串r4)?r4:x.field4
})
.Select(x=>$“{x.field1}{x.field2};{x.field3}{x.field4}”)
.ToArray();
您使用StringBuilder进行过测试吗
StringBuilder sb = new StringBuilder(string.Join(",", this.to));
string tempStr = sb
.Replace("e77f75b7-2373-dc11-8f13-0019bb2ca0a0", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
.Replace("fbd0c892-2373-dc11-8f13-0019bb2ca0a0", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
.Replace("76cd4297-1e31-dc11-95d8-0019bb2ca0a0", "eb892fb0-fe17-e811-80d8-00155d5ce473")
.Replace("cd42bb68-2073-dc11-8f13-0019bb2ca0a0", "dc6077e2-fe17-e811-80d8-00155d5ce473")
.Replace("96b97150-cd45-e111-a3d5-00155d10010f", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
.ToString();
var newArray = tempStr.Split(',');
您使用StringBuilder进行过测试吗
StringBuilder sb = new StringBuilder(string.Join(",", this.to));
string tempStr = sb
.Replace("e77f75b7-2373-dc11-8f13-0019bb2ca0a0", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
.Replace("fbd0c892-2373-dc11-8f13-0019bb2ca0a0", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
.Replace("76cd4297-1e31-dc11-95d8-0019bb2ca0a0", "eb892fb0-fe17-e811-80d8-00155d5ce473")
.Replace("cd42bb68-2073-dc11-8f13-0019bb2ca0a0", "dc6077e2-fe17-e811-80d8-00155d5ce473")
.Replace("96b97150-cd45-e111-a3d5-00155d10010f", "1fe8f3f6-fe17-e811-80d8-00155d5ce473")
.ToString();
var newArray = tempStr.Split(',');
我会使用替换词典-它更易于维护和理解(我认为),因此一路都更容易: 样板文件和创建演示数据/替换dict:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
internal class Program
{
static void Main(string[] args)
{
// c#7 inline func
string[] CreateDemoData(Dictionary<string, string> replDict)
{
// c#7 inline func
string FilText(string s) => $"Some text| that also incudes; {s} and more.";
return Enumerable
.Range(1, 5)
.Select(i => FilText(Guid.NewGuid().ToString()))
.Concat(replDict.Keys.Select(k => FilText(k)))
.OrderBy(t => Guid.NewGuid().GetHashCode())
.ToArray();
}
// replacement dict
var d = new Dictionary<string, string>
{
["e77f75b7-2373-dc11-8f13-0019bb2ca0a0"] = "e77f75b7-replaced",
["fbd0c892-2373-dc11-8f13-0019bb2ca0a0"] = "fbd0c892-replaced",
["76cd4297-1e31-dc11-95d8-0019bb2ca0a0"] = "76cd4297-replaced",
["cd42bb68-2073-dc11-8f13-0019bb2ca0a0"] = "cd42bb68-replaced",
["96b97150-cd45-e111-a3d5-00155d10010f"] = "96b97150-replaced",
};
var arr = CreateDemoData(d);
输出:
Before:
Some text| that also incudes; a5ceefd8-1388-47cd-b69e-55b6ddbbc133 and more.
Some text| that also incudes; 76cd4297-1e31-dc11-95d8-0019bb2ca0a0 and more.
Some text| that also incudes; 3311a8c5-015e-4260-af80-86b20b277234 and more.
Some text| that also incudes; ed10c79c-dad6-4c88-865c-4d7624945d66 and more.
Some text| that also incudes; 96b97150-cd45-e111-a3d5-00155d10010f and more.
Some text| that also incudes; 0226d9b1-c5f0-41fb-9294-bc9297e8afd9 and more.
Some text| that also incudes; e77f75b7-2373-dc11-8f13-0019bb2ca0a0 and more.
Some text| that also incudes; a04d1e34-e7bc-4bbc-ae0e-12ec846a353c and more.
Some text| that also incudes; cd42bb68-2073-dc11-8f13-0019bb2ca0a0 and more.
Some text| that also incudes; fbd0c892-2373-dc11-8f13-0019bb2ca0a0 and more.
输出:
After:
Some text| that also incudes; a5ceefd8-1388-47cd-b69e-55b6ddbbc133 and more.
Some text| that also incudes; 76cd4297-replaced and more.
Some text| that also incudes; 3311a8c5-015e-4260-af80-86b20b277234 and more.
Some text| that also incudes; ed10c79c-dad6-4c88-865c-4d7624945d66 and more.
Some text| that also incudes; 96b97150-replaced and more.
Some text| that also incudes; 0226d9b1-c5f0-41fb-9294-bc9297e8afd9 and more.
Some text| that also incudes; e77f75b7-replaced and more.
Some text| that also incudes; a04d1e34-e7bc-4bbc-ae0e-12ec846a353c and more.
Some text| that also incudes; cd42bb68-replaced and more.
Some text| that also incudes; fbd0c892-replaced and more.
我会使用替换词典-它更易于维护和理解(我认为),因此一路都更容易: 样板文件和创建演示数据/替换dict:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
internal class Program
{
static void Main(string[] args)
{
// c#7 inline func
string[] CreateDemoData(Dictionary<string, string> replDict)
{
// c#7 inline func
string FilText(string s) => $"Some text| that also incudes; {s} and more.";
return Enumerable
.Range(1, 5)
.Select(i => FilText(Guid.NewGuid().ToString()))
.Concat(replDict.Keys.Select(k => FilText(k)))
.OrderBy(t => Guid.NewGuid().GetHashCode())
.ToArray();
}
// replacement dict
var d = new Dictionary<string, string>
{
["e77f75b7-2373-dc11-8f13-0019bb2ca0a0"] = "e77f75b7-replaced",
["fbd0c892-2373-dc11-8f13-0019bb2ca0a0"] = "fbd0c892-replaced",
["76cd4297-1e31-dc11-95d8-0019bb2ca0a0"] = "76cd4297-replaced",
["cd42bb68-2073-dc11-8f13-0019bb2ca0a0"] = "cd42bb68-replaced",
["96b97150-cd45-e111-a3d5-00155d10010f"] = "96b97150-replaced",
};
var arr = CreateDemoData(d);
输出:
Before:
Some text| that also incudes; a5ceefd8-1388-47cd-b69e-55b6ddbbc133 and more.
Some text| that also incudes; 76cd4297-1e31-dc11-95d8-0019bb2ca0a0 and more.
Some text| that also incudes; 3311a8c5-015e-4260-af80-86b20b277234 and more.
Some text| that also incudes; ed10c79c-dad6-4c88-865c-4d7624945d66 and more.
Some text| that also incudes; 96b97150-cd45-e111-a3d5-00155d10010f and more.
Some text| that also incudes; 0226d9b1-c5f0-41fb-9294-bc9297e8afd9 and more.
Some text| that also incudes; e77f75b7-2373-dc11-8f13-0019bb2ca0a0 and more.
Some text| that also incudes; a04d1e34-e7bc-4bbc-ae0e-12ec846a353c and more.
Some text| that also incudes; cd42bb68-2073-dc11-8f13-0019bb2ca0a0 and more.
Some text| that also incudes; fbd0c892-2373-dc11-8f13-0019bb2ca0a0 and more.
输出:
After:
Some text| that also incudes; a5ceefd8-1388-47cd-b69e-55b6ddbbc133 and more.
Some text| that also incudes; 76cd4297-replaced and more.
Some text| that also incudes; 3311a8c5-015e-4260-af80-86b20b277234 and more.
Some text| that also incudes; ed10c79c-dad6-4c88-865c-4d7624945d66 and more.
Some text| that also incudes; 96b97150-replaced and more.
Some text| that also incudes; 0226d9b1-c5f0-41fb-9294-bc9297e8afd9 and more.
Some text| that also incudes; e77f75b7-replaced and more.
Some text| that also incudes; a04d1e34-e7bc-4bbc-ae0e-12ec846a353c and more.
Some text| that also incudes; cd42bb68-replaced and more.
Some text| that also incudes; fbd0c892-replaced and more.
字符串有多大?输入数组中有多少个元素?你能就地修改输入数组而不是创建一个新数组吗?@Liam我的观点是OP有一个由小字符串组成的大数组。你有没有尝试过简单的foreach循环就地修改数组元素而不是创建另一个大数组?你认为这是导致OutOfMemoryException的代码?我可以在32位.NET环境中使用一百万份示例输入运行代码,没有任何问题。字符串有多大?输入数组中有多少元素?你能就地修改输入数组而不是创建一个新数组吗?@Liam我的观点是OP有一个由小字符串组成的大数组。你有没有尝试过简单的foreach循环就地修改数组元素而不是创建另一个大数组?你认为这是导致OutOfMemoryException的代码?我能够在32位.NET环境中使用一百万份示例输入运行代码,没有任何问题。这将所有
到[]
的记录放在一个大字符串中。Replace()调用会慢得多。@HenkHolterman,但我认为它仍然比多个string.Replace()调用快。这将所有到[]
的记录放在一个大字符串中。Replace()调用会慢很多。@HenkHolterman,但我认为它仍然比多个string.Replace()调用快。为了应用此解决方案,您需要进行额外的解析。根据OP的行格式。谢谢@AlexandruClonțea-这是在事后编辑的。我们将了解如何调整此.Upvoting。我想知道Regex.Replace(“a | b | c | d | e”、“newVal”)是否会减少迭代次数。我知道regex.Replace在相同的步数下比较慢。。。我只是想知道性能是否会因为更少的整体“感知”迭代而更好。还有,我想知道为什么OP越来越流行OOM@AlexandruClonțea我只为linq之前的每个现有关键礼节更换一次。Regex.Replace with or'ed条件可能不起作用,因为每个guid都由不同的内容进行了应答,所以您还必须使用多个单次替换。如果一个键在字符串中,那么要得到的Linq仍然可以使用,但是接下来您可以测试string.Replace()和regex.Replace-我喜欢regex,但有时只使用内置的更容易。如果同一行中的两个guid被替换,那么您就需要使用另一个循环,这样就不那么容易了。在OP的示例中,我觉得目标guid是相同的。我想象