c#追加字典替换的结果

标签 c# string dictionary replace

我有一本字典,其中包含缩写词的 key 和完整含义的 value。这被命名为 WordDictionary,保存在名为 fileParser 的类中。

我正在尝试在字符串中附加一个完整含义的缩写词。我可以找到并用完整含义替换该词,但是有没有一种方法可以将完整含义附加在缩写词之后,如下所示。

some text with the abbreviation LOL

将更改为。

some text with the abbreviation LOL (Laughing Out Loud)

我要替换缩写的代码如下。

foreach (string s in isInFile)
            {
                StringBuilder expandedTXT = new StringBuilder(inMessage);
                //test for reading words from TB 
                //Console.WriteLine(s);
                if (fileParser.IsWordAvailable(s))
                {
                    Console.WriteLine(s);// displays word checked in console
                    foreach(var word in fileParser.WordDictionary)
                    {
                        expandedTXT.Replace(word.Key, word.Value);
                    }
                    MessageBox.Show("Match found: " + s);
                    MessageBox.Show("New String: " + expandedTXT.ToString());
                }
            }

最佳答案

您可以将值替换为任何您想要的值,不必只使用单个变量的值。 IE。您可以使用字符串连接或最好的格式从缩写和完整含义构造所需的子字符串:

  expandedTXT.Replace(word.Key, 
        String.Format("{0} ({1})", word.Key, word.Value));

关于c#追加字典替换的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47418876/

相关文章:

java - 在 Java 8 中合并递归 Hashmap

python - 如何填充已在python中创建的空字符串

dictionary - Xquery 中的字典

c# - 尝试编写一个令人愉快的解析器

C#、Metro、Stream Socket、SSL 不可信主机、Squid

android - 我在哪里可以浏览 java.lang.String 的 Android 2.2 Froyo 源代码?

Java初学者: Is this best practise (randoms and conditionals)?

php - 我正在制作一个带有 map 的 PHP 游戏,我有一些问题

c# - WebRequest GetResponseStream 读取字节

c# - SQL Server 2008 R2 的 C# CLR 中的 newtonsoft.json 解析器 - 如何部署?