C# String replace with dictionary

2019-01-13 13:39发布

问题:

I have a string on which I need to do some replacements. I have a Dictionary<string, string> where I have search-replace pairs defined. I have created following extension methods to perform this operation:

public static string Replace(this string str, Dictionary<string, string> dict)
{
    StringBuilder sb = new StringBuilder(str);

    return sb.Replace(dict).ToString();
}

public static StringBuild Replace(this StringBuilder sb, 
    Dictionary<string, string> dict)
{
    foreach (KeyValuePair<string, string> replacement in dict)
    {
        sb.Replace(replacement.Key, replacement.Value);
    }

    return sb;
}

Is there a better way of doing that?

回答1:

If the data is tokenized (i.e. "Dear $name$, as of $date$ your balance is $amount$"), then a Regex can be useful:

static readonly Regex re = new Regex(@"\$(\w+)\$", RegexOptions.Compiled);
static void Main() {
    string input = @"Dear $name$, as of $date$ your balance is $amount$";

    var args = new Dictionary<string, string>(
        StringComparer.OrdinalIgnoreCase) {
            {"name", "Mr Smith"},
            {"date", "05 Aug 2009"},
            {"amount", "GBP200"}
        };
    string output = re.Replace(input, match => args[match.Groups[1].Value]);
}

However, without something like this, I expect that your Replace loop is probably about as much as you can do, without going to extreme lengths. If it isn't tokenized, perhaps profile it; is the Replace actually a problem?



回答2:

Do this with Linq:

var newstr = dict.Aggregate(str, (current, value) => 
     current.Replace(value.Key, value.Value));

dict is your search-replace pairs defined Dictionary object.

str is your string which you need to do some replacements with.



回答3:

Seems reasonable to me, except for one thing: it's order-sensitive. For instance, take an input string of "$x $y" and a replacement dictionary of:

"$x" => "$y"
"$y" => "foo"

The results of the replacement are either "foo foo" or "$y foo" depending on which replacement is performed first.

You could control the ordering using a List<KeyValuePair<string, string>> instead. The alternative is to walk through the string making sure you don't consume the replacements in further replace operations. That's likely to be a lot harder though.



回答4:

Here's a lightly re-factored version of @Marc's great answer, to make the functionality available as an extension method to Regex:

static void Main() 
{
    string input = @"Dear $name$, as of $date$ your balance is $amount$";
    var args = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
    args.Add("name", "Mr Smith");
    args.Add("date", "05 Aug 2009");
    args.Add("amount", "GBP200");

    Regex re = new Regex(@"\$(\w+)\$", RegexOptions.Compiled);
    string output = re.replaceTokens(input, args);

    // spot the LinqPad user // output.Dump();
}

public static class ReplaceTokensUsingDictionary
{
    public static string replaceTokens(this Regex re, string input, IDictionary<string, string> args)
    {
        return re.Replace(input, match => args[match.Groups[1].Value]);
    }
}


回答5:

when using Marc Gravell's RegEx solution, first check if a token is available using i.e. ContainsKey, this to prevent KeyNotFoundException errors :

string output = re.Replace(zpl, match => { return args.ContainsKey(match.Groups[1].Value) ? arg[match.Groups[1].Value] : match.Value; });

when using the following slightly modified sample code (1st parameter has different name):

    var args = new Dictionary<string, string>(
        StringComparer.OrdinalIgnoreCase) 
        {
            {"nameWRONG", "Mr Smith"},
            {"date", "05 Aug 2009"},
            {"AMOUNT", "GBP200"}
        };

this produces the following:

"Dear $name$, as of 05 Aug 2009 your balance is GBP200"



回答6:

Here you are:

public static class StringExm
{
    public static String ReplaceAll(this String str, KeyValuePair<String, String>[] map)
    {
        if (String.IsNullOrEmpty(str))
            return str;

        StringBuilder result = new StringBuilder(str.Length);
        StringBuilder word = new StringBuilder(str.Length);
        Int32[] indices = new Int32[map.Length];

        for (Int32 characterIndex = 0; characterIndex < str.Length; characterIndex++)
        {
            Char c = str[characterIndex];
            word.Append(c);

            for (var i = 0; i < map.Length; i++)
            {
                String old = map[i].Key;
                if (word.Length - 1 != indices[i])
                    continue;

                if (old.Length == word.Length && old[word.Length - 1] == c)
                {
                    indices[i] = -old.Length;
                    continue;
                }

                if (old.Length > word.Length && old[word.Length - 1] == c)
                {
                    indices[i]++;
                    continue;
                }

                indices[i] = 0;
            }

            Int32 length = 0, index = -1;
            Boolean exists = false;
            for (int i = 0; i < indices.Length; i++)
            {
                if (indices[i] > 0)
                {
                    exists = true;
                    break;
                }

                if (-indices[i] > length)
                {
                    length = -indices[i];
                    index = i;
                }
            }

            if (exists)
                continue;

            if (index >= 0)
            {
                String value = map[index].Value;
                word.Remove(0, length);
                result.Append(value);

                if (word.Length > 0)
                {
                    characterIndex -= word.Length;
                    word.Length = 0;
                }
            }

            result.Append(word);
            word.Length = 0;
            for (int i = 0; i < indices.Length; i++)
                indices[i] = 0;
        }

        if (word.Length > 0)
            result.Append(word);

        return result.ToString();
    }
}