用另一个替换单词集

时间:2013-09-30 05:08:03

标签: c# replace

我知道如何使用以下代码替换段落或文本文件中的另一个特定单词:

String output = input.Replace("oldvalue","newvalue");

但我对替换替换单词组感到困惑。我有近1000个单词要替换。 E.g:

" aback " => " ashamed ",
" abacus " => " abacus ",
" abaft " => " aback ",
" abandon " => " carelessness ",
" abandoned " => " alone ",

因此,如果该段落包含单词aback,我想将其替换为ashamed,就像每个单词一样。我们怎么做?谁能给我一个想法?

3 个答案:

答案 0 :(得分:10)

您可以编写像这样的扩展方法

public static string ReplaceSetOfStrings(this string input, Dictionary<string, string> pairsToReplace)
{
    foreach (var wordToReplace in pairsToReplace)
    {
        input = input.Replace(wordToReplace.Key, wordToReplace.Value);
    }
    return input;
}

在上面的方法中,Dictionary键将包含需要用值重新替换的单词替换的单词。

然后你就可以这样称呼它

Dictionary<string,string> pairsToBeReplaced = new Dictionary<string,string>();
pairsToBeReplaced.Add(" aback "," ashamed ");
input.ReplaceSetOfStrings(pairsToBeReplaced);

答案 1 :(得分:1)

由于您试图替换实际单词,因此您可能只找到完整单词(而不是部分单词),即使它们位于句子的开头(即大写)或句子的结尾(例如,他们之后有一段时间)。这需要一个正则表达式,因为你可以告诉它只用\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编译为可能的优化。

答案 2 :(得分:0)

您可以替换如下:Stolen from Here

StringBuilder sb = new StringBuilder("11223344");

    string myString =
        sb
          .Replace("1", string.Empty)
          .Replace("2", string.Empty)
          .Replace("3", string.Empty)
          .ToString();

或者您也可以这样做:Stolen from Here

// Define name/value pairs to be replaced.
var replacements = new Dictionary<string,string>();
replacements.Add("<Name>", client.FullName);
replacements.Add("<EventDate>", event.EventDate.ToString());

// Replace
string s = "Dear <Name>, your booking is confirmed for the <EventDate>";
foreach (var replacement in replacements)
{
   s = s.Replace(replacement.Key, replacement.Value);
}