由于您正在尝试替换实际单词,因此仅找到完整的单词(而不是部分单词)可能很重要,即使它们位于句子的开头(即大写)或句子的结尾(例如有句点)在他们之后)。这需要一个正则表达式,因为您可以告诉它只查找带有\b
.
// This version will replace whole words (you don't need spaces around each one)
string ReplaceWords(string input, Dictionary<string, string> replacements)
{
return Regex.Replace(
input,
@"\b("
+ String.Join("|", replacements.Keys.Select(k => Regex.Escape(k)))
+ @")\b", // pattern
m => replacements[m.Value] // replacement
);
}
// This version will replace capitalized words if the key is all lowercase
string ReplaceWords(string input, Dictionary<string, string> replacements)
{
return Regex.Replace(
input,
@"\b("
+ String.Join("|", replacements.Keys.Select(k => Regex.Escape(k)))
+ @")\b", // pattern
m => replacements[m.Value], // replacement
RegexOptions.IgnoreCase);
}
private static string ReplaceWord(string word, Dictionary<string, string> replacements)
{
string replacement;
// see if word is in the dictionary as-is
if (replacements.TryGetValue(word, out replacement))
return replacement;
// see if the all-lowercase version is in the dictionary
if (replacements.TryGetValue(word.ToLower(), out replacement))
{
// if the word is all uppercase, make the replacement all uppercase
if (word.ToUpper() == word)
return replacement.ToUpper();
// if the first letter is uppercase, make the replacement's first letter so
if (char.IsUpper(word[0]))
return char.ToUpper(replacement[0]) + replacement.Substring(1);
// otherwise just return the replacement as-is
return replacement;
}
// no replacement found, so don't replace
return word;
}
如果字典中的键在调用之间没有变化,您可以将其编译Regex
为可能的优化。