Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#追加字典替换结果_C#_String_Dictionary_Replace - Fatal编程技术网

c#追加字典替换结果

c#追加字典替换结果,c#,string,dictionary,replace,C#,String,Dictionary,Replace,我有一本字典,里面有一个缩写词的关键字,还有一个值完整的意思。它被命名为WordDictionary,保存在名为fileParser的类中 我试图在一个字符串中附加一个缩写词full-means with。我可以找到并用完整的意思替换这个词,但是有没有一种方法可以将完整的意思附加在缩写词之后,如下所示 foreach (string s in isInFile) { StringBuilder expandedTXT = new Strin

我有一本字典,里面有一个缩写词的
关键字
,还有一个
完整的意思。它被命名为
WordDictionary
,保存在名为
fileParser
的类中

我试图在一个字符串中附加一个缩写词full-means with。我可以找到并用完整的意思替换这个词,但是有没有一种方法可以将完整的意思附加在缩写词之后,如下所示

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());
                }
            }
一些带有缩写LOL的文本

将更改为

一些带有缩写LOL的文本(大声笑)

我必须替换缩写的代码如下

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());
                }
            }

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

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