gpt4 book ai didi

c# - 如何使用 StringBuilder 进行不区分大小写的替换

转载 作者:行者123 更新时间:2023-11-30 19:58:55 25 4
gpt4 key购买 nike

我有一个(大)模板,想替换多个值。替换需要不区分大小写。还必须可以有模板中不存在的键。

例如:

[TestMethod]
public void ReplaceMultipleWithIgnoreCaseText()
{
const string template = "My name is @Name@ and I like to read about @SUBJECT@ on @website@, tag @subject@";
const string expected = "My name is Alex and I like to read about C# on stackoverflow.com, tag C#";
var replaceParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("@name@","Alex"),
new KeyValuePair<string, string>("@subject@","C#"),
new KeyValuePair<string, string>("@website@","stackoverflow.com"),
// Note: The next key does not exist in template
new KeyValuePair<string, string>("@country@","The Netherlands"),
};
var actual = ReplaceMultiple(template, replaceParameters);
Assert.AreEqual(expected, actual);
}

public string ReplaceMultiple(
string template,
IEnumerable<KeyValuePair<string, string>> replaceParameters)
{
throw new NotImplementedException(
"Implementation needed for many parameters and long text.");
}

请注意,如果我有一个包含 30 个参数的列表和一个大模板,我不希望内存中有 30 个大字符串。使用 StringBuilder 似乎是一种选择,但也欢迎使用其他解决方案。

我试过但没有用的解决方案

此处找到的解决方案 (C# String replace with dictionary) 在集合中不存在键时抛出异常,但我们的用户犯了错误,在这种情况下,我只想将 wromg 键留在文本中。示例:

static readonly Regex re = new Regex(@"\$(\w+)\$", RegexOptions.Compiled);
static void Main2()
{
// "Name" is accidentally typed by a user as "nam".
string input = @"Dear $nam$, as of $date$ your balance is $amount$";

var args = new Dictionary<string, string>(
StringComparer.OrdinalIgnoreCase) {
{"name", "Mr Smith"},
{"date", "05 Aug 2009"},
{"amount", "GBP200"}};


// Works, but not case insensitive and
// uses a lot of memory when using a large template
// ReplaceWithDictionary many args
string output1 = input;
foreach (var arg in args)
{
output1 = output1.Replace("$" + arg.Key +"$", arg.Value);
}

// Throws a KeyNotFoundException + Only works when data is tokenized
string output2 = re.Replace(input, match => args[match.Groups[1].Value]);
}

最佳答案

Using a StringBuilder seems to be an option, but other solutions are also welcome.

因为你想要不区分大小写,我建议(非 StringBuilder):

public static string ReplaceMultiple(
string template,
IEnumerable<KeyValuePair<string, string>> replaceParameters)
{
var result = template;

foreach(var replace in replaceParameters)
{
var templateSplit = Regex.Split(result,
replace.Key,
RegexOptions.IgnoreCase);
result = string.Join(replace.Value, templateSplit);
}

return result;
}

DotNetFiddle Example

关于c# - 如何使用 StringBuilder 进行不区分大小写的替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com