1

我有以下类型的字符串格式---

Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}

该字符串包含一对具有不同位置的{ }并且可能是 n 次。现在我想用我将根据{}对之间的字符串计算的其他字符串替换该对。

这该怎么做 ?

4

4 回答 4

1

你可以试试正则表达式。具体来说,Regex.Replace使用变体MatchEvaluator应该可以解决问题。有关详细信息,请参阅http://msdn.microsoft.com/en-US/library/cft8645c(v=vs.80).aspx

这些方面的东西:

using System;
using System.Text.RegularExpressions;

public class Replacer
{
    public string Replace(string input)
    {
        // The regular expression passed as the second argument to the Replace method
        // matches strings in the format "{value0#value1/value2}", i.e. three strings
        // separated by "#" and "/" all surrounded by braces.
        var result = Regex.Replace(
            input,
            @"{(?<value0>[^#]+)#(?<value1>[^/]+)/(?<value2>[^}]+)}",
            ReplaceMatchEvaluator);
        return result;
    }

    private string ReplaceMatchEvaluator(Match m)
    {
        // m.Value contains the matched string including the braces.
        // This method is invoked once per matching portion of the input string.
        // We can then extract each of the named groups in order to access the
        // substrings of each matching portion as follows:
        var value0 = m.Groups["value0"].Value; // Contains first value, e.g. "Jwala Vora"
        var value1 = m.Groups["value1"].Value; // Contains second value, e.g. "3"
        var value2 = m.Groups["value2"].Value; // Contains third value, e.g. "13"

        // Here we can do things like convert value1 and value2 to integers...
        var intValue1 = Int32.Parse(value1);
        var intValue2 = Int32.Parse(value2);

        // etc.

        // Here we return the value with which the matching portion is replaced.
        // This would be some function of value0, value1 and value2 as well as
        // any other data in the Replacer class.
        return "xyz";
    }
}

public static class Program
{
    public static void Main(string[] args)
    {
        var replacer = new Replacer();
        var result = replacer.Replace("Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}");
        Console.WriteLine(result);
    }
}

该程序将输出Proposal is given to xyz for xyz xyz by xyz.

您需要在ReplaceMatchEvaluator要处理的方法中提供特定于应用程序的逻辑value0value1value2酌情提供。该类Replacer可以包含可用于实现替换逻辑的附加成员ReplaceMatchEvaluatorReplace通过调用类的实例来处理字符串Replacer

于 2013-02-08T07:32:05.803 回答
0

那么你可以用'{'和'}'分割字符串并以这种方式确定内容。

但我认为更好的方法是按索引查找字符,然后您知道一对或大括号的起始索引和结束索引,这样您就可以用替换的占位符重建字符串。

但是最好的方法可能是使用 Regex.Replace 但这只会有助于用您想要的值替换占位符,但我认为您的要求是还解析大括号内的文本并基于该值选择要插入的值所以这可能效果不好。使用通配符类型搜索查找和替换字符串的一部分

于 2013-02-08T07:31:47.313 回答
0

您可以使用Regex.Replace Method (String, String, MatchEvaluator)方法和{.*?}模式。以下示例使用字典替换值,但您可以用自己的逻辑替换它。

class Program
{
    static Dictionary<string, string> _dict = new Dictionary<string, string>();

    static void Main(string[] args)
    {
        _dict.Add("{Jwala Vora#3/13}","someValue1");
        _dict.Add("{Amazon Vally#2/11}", "someValue2");
        _dict.Add("{1#3/75}", "someValue3");
        _dict.Add("{MdOffice employee#1/1}", "someValue4");

        var input = @"Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}";

        var result = Regex.Replace(input, @"{.*?}", Evaluate);

        Console.WriteLine(result);
    }

    private static string Evaluate(Match match)
    {
        return _dict[match.Value];
    }
}
于 2013-02-08T07:42:04.180 回答
-1

你不能做点什么string.Format()吗?

例如

string.Format("Proposal is given to {0} for {1} {2} by {3}", "Jwala Vora", "Amazon Vally", 1, "MdOffice employee");
于 2013-02-08T07:35:16.333 回答