我有一本字典,其中包含缩写词的 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/