比如有一个字符串是:“aaa{参数}bbb{参数}{参数}”
然后有三个值“123”,“234”,“ttt”
怎么实现如下的效果,用值顺序替换掉字符串中的参数:
“aaa123bbb234ttt”用正则表达式是不是好实现,但是我没接触过,还请各位大侠帮忙,

解决方案 »

  1.   

    你直接修改参数的值不行吗?然后再连接。感觉你的替换很简单,不用正则吧,而且用replace()方法也可以啊
      

  2.   

    你这个无规则的么直接连续replace啊
      

  3.   

    http://msdn.microsoft.com/zh-cn/library/2k3te2cs.aspx
      

  4.   


    string a = "123";
    string b = "234";
    string c = string.Format("{0},{1}", a, b);
      

  5.   

    string s = string.Format("aaa{0}bbb{1}{2}", "123", "234", "ttt");
    喵~
      

  6.   

    都是三位数吗?
    如果都是三位好判断如果不定长度不好弄,还有就是
    “aaa123bbb234ttt
    如果不定长度,234和ttt怎么区分...
      

  7.   

    string s = string.Format("aaa{0}bbb{1}{2}", "123", "234", "ttt");
    同上
      

  8.   


    string str="";//要替换的字符串
    Dictionary<string, string> data = new Dictionary<string, string>();
    //然后以参数名作为key,参数值作为value添加进去后
    foreach(string key in data.Keys)
    {
        str=str.Replace("{"+key+"}",data[key]);
    }
      

  9.   


    key是一样的,恐怕这种方法是不可以的
      

  10.   

                string[] value = { "123", "234", "ttt" };
                string str = "aaa{参数}bbb{参数}{参数}";
                string[] strCollection = Regex.Split(str, @"\{[^}]+\}");
                string result = string.Empty;
                for (var i = 0; i < value.Length; i++)
                {
                    result += (i > strCollection.Length ? "" : strCollection[i]) + value[i];
                }
                Console.WriteLine(result);