-1

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

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

但我对替换一组单词感到困惑。我有将近 1000 个单词要替换。例如:

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

因此,如果该段落包含aback我想为每个单词替换它的ashamed单词。我们怎么能做到这一点?谁能给我一个想法?

4

3 回答 3

11

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

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<string,string> pairsToBeReplaced = new Dictionary<string,string>();
pairsToBeReplaced.Add(" aback "," ashamed ");
input.ReplaceSetOfStrings(pairsToBeReplaced);
于 2013-09-30T05:16:52.710 回答
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为可能的优化。

于 2013-09-30T05:57:58.197 回答
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();

或者你也可以这样做: 从这里偷走

// 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);
}
于 2013-09-30T05:13:38.333 回答