C# 根据给定的“合并字典值的最佳方法是什么?”;“关键”;一串

C# 根据给定的“合并字典值的最佳方法是什么?”;“关键”;一串,c#,dictionary,C#,Dictionary,我有以下字典: Dictionary<string, string> d = new Dictionary<string, string>(); d.Add("ship", "I"); d.Add("shep", "V"); d.Add("ssed", "X"); d.Add("aspe", "L"); Dictionary d=newdictionary(); d、 添加(“船舶”、“I”); d、 添加(“shep”、“V”); d、 加上(“经修订的”、“X”);

我有以下字典:

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("ship", "I");
d.Add("shep", "V");
d.Add("ssed", "X");
d.Add("aspe", "L");
Dictionary d=newdictionary();
d、 添加(“船舶”、“I”);
d、 添加(“shep”、“V”);
d、 加上(“经修订的”、“X”);
d、 添加(“aspe”、“L”);
下面是输入文本行: 字符串line=“shep ship”

我怎样才能最好地将上面的单词(shep、ship和ship)从上面的字典转换成合适的罗马数字。对于上述行,应显示为VII(shep ship ship)

Dictionary dCost=new Dictionary();
dCost.Add(“aspe aspe MaterialType1”,64);
D成本加上(“材料类型2”,5880);
我想将上面的dCost字典键
aspe aspe MaterialType1
转换为第一个字典中相应的罗马数字 因此,以上两行应转换为
LL MaterialType1
和另一行“XX MaterialType2”。也可以在新字典上获得结果,或者只访问字典的第一个元素来获取/解析罗马映射

需要:目前,我一直在传递罗马值以转换其相关值,但现在,我将在字典中输入上面提供的罗马数字映射。因此,我需要根据给定的输入获取适当的数字,并将其传递给API,以便将罗马数字转换为数字。

有谁能给我建议将这些字典与其映射值合并的最佳方法吗?这对于linq或任何方法都是好的


谢谢你

你想要什么真的不太清楚,但我想这至少会有帮助:

public static string ReplaceAll(string text,
                                Dictionary<string, string> replacements)
{
    foreach (var pair in replacements)
    {
        text = text.Replace(pair.Key, pair.Value);
    }
    return text;
}
publicstaticstringreplaceall(字符串文本,
字典替换)
{
foreach(替换中的var对)
{
text=text.Replace(pair.Key,pair.Value);
}
返回文本;
}
注:

  • 如果“shep”(etc)可能出现在真实文本中,那么这不会满足您的需要。您可能希望使用正则表达式仅对单词边界执行替换
  • 这将当前保留输入中的空格,因此最终将使用“L MaterialType1”而不是“LL MaterialType1”
简单案例 如果我们假设成本关键字始终以单个单词结尾(即MaterialType1),则关键字中的最后一个空格将要翻译的文本与材料类型名称分隔开

例如:

“aspe aspe材料类型1”

要翻译此字符串,可以使用类似于此代码段的内容

foreach(var cost in dCosts)
{
    int lastSpaceIndex = cost.Key.LastIndexOf(" ");
    string materialTypeName = cost.Key.Substring(lastSpaceIndex + 1)
                                      .Trim();
    string translatedKey = cost.Key.Substring(0, lastSpaceIndex);
    foreach (var translation in d)
    {
        translatedKey = translatedKey.Replace(translation.Key, translation.Value)
                                     .Trim();
    }

    translatedKey = translatedKey.Replace(" ", string.Empty);       

    Console.WriteLine("{0} {1} cost is {2}", 
                      translatedKey,
                      materialTypeName,
                      cost.Value);
}
复杂情况 请把它当作例子。您可以按如下方式实现“翻译”字符串键

foreach(var cost in dCosts)
{
    string translatedKey = cost.Key;
    foreach (var translation in d)
    {
        translatedKey = translatedKey.Replace(translation.Key, translation.Value)
                                     .Trim();
    }

    Console.WriteLine("{0} cost is {1}", translatedKey, cost.Value);
}

正如@JonSkeet在他的回答中所指出的,使用这个代码片段,您可以在“翻译”的值之间保留空格,因此这并不是对这种情况的真正回答。

不清楚第二个字典是什么,或者您希望结果在哪里。它们应该在另一本字典中吗?@JonSkeet是的,在新字典上也可以,或者只访问字典的第一个元素来获取/解析罗马映射。谢谢我想你需要
translatedKey。替换
而不是
cost.Key。替换
我同意!非常感谢。谢谢你的评论。如何从字典中获取匹配值,以便
shep ship
所以,结果应该是VII(没有从字典中获取空格和值)。若能通过regex/linq找到一些方法,那个就太好了。更新的问题中有一些我需要的背景细节。@Dhaval:老实说,在空格方面会很棘手。如果你总是知道会有3个领先的条款,那还不算太坏。。。但一般情况更难。
foreach(var cost in dCosts)
{
    string translatedKey = cost.Key;
    foreach (var translation in d)
    {
        translatedKey = translatedKey.Replace(translation.Key, translation.Value)
                                     .Trim();
    }

    Console.WriteLine("{0} cost is {1}", translatedKey, cost.Value);
}