c# - 我的字典 wp 应用程序在 c#

标签 c# dictionary windows-phone-8.1

我想在经常写这个词的时候..再重复一遍

像这样: 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/

相关文章:

c# - 使用 .First() 和 .Where().First() 的区别

c# - 访问 .NET Core 控制台应用程序中的 System.Printing 命名空间

c# - 在模型更改时保留数据库内容

c# - 基于字典排序 IEnumerable<T> 对象

c# - 如何有效地削减字典中的词条数量?

visual-studio - Windows Phone 8.1 模拟器卡在启动操作系统

python - 通过 POST multipart/form-data 从 windows phone 8.1 Silverlight Httpclient 上传文件到 python 服务器损坏文件

C# - 我需要 list 文件吗?

c# - 检测哈希表或字典中的重新散列或冲突

javascript - 如何使用 WinJS 在 Windows Phone 8.1 中保存图像