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