我想在经常写这个词的时候..再重复一遍
像这样: textbox1“法国 - 约旦 - 法国”... 并显示“巴黎 - 安曼 - 巴黎”
但在此代码中只显示一次“巴黎 - 安曼”
private void Button_Click(object sender, RoutedEventArgs e)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("France", "Paris");
dictionary.Add("England", "London");
dictionary.Add("Jordan", "Amman");
textbox2.Text = "";
if (textbox1.Text.Contains("France"))
{
string value = dictionary["France"];
textbox2.Text += value;
}
if (textbox1.Text.Contains("England"))
{
string value = dictionary["England"];
textbox2.Text += value;
}
if (textbox1.Text.Contains("Jordan"))
{
string value = dictionary["Jordan"];
textbox2.Text += value;
}
}
最佳答案
如果你的字典很小(少于几百个元素),你可以复制你的字符串,然后替换所有出现的地方:
private void Button_Click(object sender, RoutedEventArgs e)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("France", "Paris");
dictionary.Add("England", "London");
dictionary.Add("Jordan", "Amman");
var result = textbox1.Text;
foreach (var entry in dictionary)
{
result.Replace(entry.Key, entry.Value);
}
textbox2.Text = result;
}
此解决方案的好处是您可以保留原始字符串的准确格式。例如,如果您的输入是 ***France***, $$$Jordan$$$
,您的输出将是 ***Paris***, $$$Amman$ $$
。缺点是它的执行速度比有针对性的搜索慢,就像 Joel Legaspi Enriquez 的回答一样。但是,如果您的字典不大,它应该不会产生任何明显的差异(最多几毫秒)。
关于c# - 我的字典 wp 应用程序在 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31845719/