如何在C#中操作包含不同模式的字符串?

时间:2013-02-08 07:28:55

标签: c# string

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

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

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

怎么做?

4 个答案:

答案 0 :(得分:1)

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

这些方面的东西:

MatchEvaluator

此程序将输出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方法中提供特定于应用的逻辑,以便根据需要处理ReplaceMatchEvaluatorvalue0value1。类value2可以包含可用于在Replacer中实现替换逻辑的其他成员。通过在ReplaceMatchEvaluator类的实例上调用Replace来处理字符串。

答案 1 :(得分:0)

你可以用'{'和'}'分割字符串并确定那些内容。

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

但最好的方法可能是使用Regex.Replace,但这只会帮助用你想要的值替换占位符,但我认为你的要求是也解析大括号内的文本,并根据它选择值到插入所以这可能不会很好。 Find and Replace a section of a string with wildcard type search

答案 2 :(得分: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];
    }
}

答案 3 :(得分:-1)

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

例如

string.Format("Proposal is given to {0} for {1} {2} by {3}", "Jwala Vora", "Amazon Vally", 1, "MdOffice employee");