假如说现在有一个字符串:
{someone}'s wallet was stolen by {someone else} but the police who called {anyone} caught him.然后还有一个字符集合:List<string> people = new List<string> { "Tom", "Jack", "Charlie"};
要做的就是创建一个函数,把字符串中用{xxx}包围的地方替换成List里面的字符。
public string ReplaceItems(string str, List<string> items)
{
//....
}输出结果为:
Tom's wallet was stolen by Jack but the police who called Charlie caught him.字符串中{}数量不一定是3个,但{}的数量和对应的List集合size是相同的,{xxx}中的字符也不固定。

解决方案 »

  1.   

    Regex reg = new Regex(@"(?<=\{)[^{}]+(?=\})");
    reg.Replace(input, "");
      

  2.   

         string a = "{someone}'s wallet was stolen by {someone else} but the police who called {anyone} caught him.";
          List<string> people = new List<string> { "Tom", "Jack", "Charlie"};
          a = a.Replace("{someone}",people[0]).Replace("{someone else}",people[1]).Replace("{anyone}",people[2]);
          Response.Write(a);
      

  3.   

    谢谢你的回答,这个方法对我举的例子是可行的,但问题是{xxx}中的字符并不一定就是{someone},{anyone},也可能是{abc}, {def}等其它的,所以并不适用于所有情况。
      

  4.   

    将字符串转换成数组,然后遍历一次,从发现{开始将要替换的对象往stringbuilder里装,直到发现第一个}。需要设置一个flag来检查{ }是否成对出现。如果不成对出现或先遇到}都要返回提示信息。即可实现
      

  5.   

    当然发现{前的字符也需要往stringbuilder里加
      

  6.   

            public string ReplaceItems(string str, List<string> items)
            {
                int l = 0, r = 0;
                int n = items.Count;
                string temp = "";
                for (int i = 0; i < n; i++)
                {
                    l = str.IndexOf("{", r);
                    temp += str.Substring(r, l - r);
                    temp += items[i];
                    r = str.IndexOf("}", l) + 1;
                }
                temp += str.Substring(r, str.Length - r);
                return temp;
            }
      

  7.   

    给你写了个例子。using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;class Program
    {
        static void Main(string[] args)
        {
            string s = "{someone}'s wallet was stolen by {someone else} but the police who called {anyone} caught him.";
            Dictionary<string, string> rule = new Dictionary<string, string>();
            rule.Add("someone", "Tom");
            rule.Add("someone else", "Jack");
            rule.Add("anyone", "Charlie");
            string result = RuleReplace(s, rule);
            Console.WriteLine(result);
            Console.ReadKey();
        }    /// <summary>
        /// 特定的字符串替换方法
        /// </summary>
        /// <param name="src">原字符串</param>
        /// <param name="rule">替换规则</param>
        /// <returns>替换后的结果字符串</returns>
        public static string RuleReplace(string src, Dictionary<string, string> rule)
        {
            return Regex.Replace(src, @"\{([^{}]+)\}", delegate(Match m)
            {
                if (rule.ContainsKey(m.Groups[1].Value))
                {
                    return rule[m.Groups[1].Value];//返回替换掉的字符串
                }
                return m.Value;//原样返回
            });
        }
    }
      

  8.   

    你只需要自己配置rule这个字典就可以了。
      

  9.   

      public string ReplaceItems(string str, List<string> items)
      {
        int k = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.Length; i++)
        {
          if (str[i] == '{')
          {
            while (i < str.Length && str[i] != '}') i++;
            sb.Append(items[k++]);
          }
          else if (str[i] != '}') sb.Append(str[i]);
        }
        return sb.ToString();
      }
      

  10.   

       List<string> people = new List<string> { "Tom", "Jack", "Charlie"};
       int i= 0;
       while (a.Contains("{"))  //循环替换所有的“{”及内部内容。
                {
                    
                    string oldStr = strTemp.Substring(a.IndexOf("{"), a.IndexOf("}") - a.IndexOf("{") + 1);  //获取第一个 {xxx}                a= a.Replace(oldStr, people[i]);
                    i++;
                    }
      

  11.   

    谢谢各位的回答,我最后用的方法:public string ReplaceItems(string str, List<string> items)
    {
        string textToReplace = str;
        Regex reg = new Regex(@"\{([^{}]+)\}");    foeach(string item in items)
        {
            textToReplace = reg.Replace(textToReplace, item, 1);
        }    return textToReplace;
    }