C# 字符串替换为字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1231768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:59:37  来源:igfitidea点击:

C# String replace with dictionary

c#stringdictionaryreplace

提问by RaYell

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:

我有一个字符串,我需要对其进行一些替换。我有一个Dictionary<string, string>定义了搜索替换对的地方。我创建了以下扩展方法来执行此操作:

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?

有没有更好的方法来做到这一点?

采纳答案by Marc Gravell

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

如果数据被标记化(即“亲爱的 $name$,截至 $date$,您的余额为 $amount$”),那么 aRegex可能很有用:

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 Replaceloop is probably about as much as you can do, without going to extreme lengths. If it isn't tokenized, perhaps profile it; is the Replaceactually a problem?

但是,如果没有这样的事情,我希望您的Replace循环可能会尽可能多,而不会太长。如果它没有被标记化,也许可以对其进行分析;是Replace实际上是一个问题吗?

回答by Jon Skeet

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”字符串和一个替换字典:

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

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

替换的结果“foo foo”或“$y foo”,具体取决于首先执行哪个替换。

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.

您可以使用 aList<KeyValuePair<string, string>>来控制排序。另一种方法是遍历字符串,确保在进一步的替换操作中不会消耗替换。不过,这可能要困难得多。

回答by Allen Wang

Do this with Linq:

使用 Linq 执行此操作:

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

dictis your search-replace pairs defined Dictionary object.

dict是您的搜索替换对定义的 Dictionary 对象。

stris your string which you need to do some replacements with.

str是您需要对其进行一些替换的字符串。

回答by Francis Norton

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

这是@Marc 很好的答案的轻微重构版本,使该功能可用作 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]);
    }
}

回答by DannyB

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

使用 Marc Gravell 的 RegEx 解决方案时,首先使用 ie ContainsKey 检查令牌是否可用,以防止 KeyNotFoundException 错误:

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"

“亲爱的 $name$,截至 2009 年 8 月 5 日,您的余额为 200 英镑”

回答by Albeoris

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