有字符串如下:
APP001□1□20APP002□1□30APP001□1□30APP001□2□30APP002□1□50APP002□2□30■希望得到的字符串为:APP001□1□50■APP001□2□30■APP002□1□80■APP002□2□30■上面的只是一部分,其实自己都看的眼花了,
言下之意就是若有重复(前两项一样)        前两项合并,最后一项相加;不明白的可以用String.Split()方法拆分一下string[] value = content.Split('■');//拆分成项//拆分成小项
for (int i = 0; i < value.Length - 1; i++)
{
   string SubscribeNo = value[i].Split('□')[0].Trim();//编号1
   string serialno = value[i].Split('□')[1].Trim();//编号2
   string Count = value[i].Split('□')[2].Trim();//数量
}我只能做到这里了,再次说明如果有两条或两条以上 编号1 和 编号2 相等的 希望把它和成 一条数据,合成后的数量为相同项的和!!!!!!!!!!!!!!
高手请指点!!!

解决方案 »

  1.   


                string str = "APP001□1□20■APP002□1□30■APP001□1□30■APP001□2□30■APP002□1□50■APP002□2□30■";
                Dictionary<string, int> dic = new Dictionary<string, int>();
                foreach (string s in str.Split(new char[] { '■' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    string key = s.Substring(0, s.LastIndexOf('□'));
                    int value = int.Parse(s.Substring(s.LastIndexOf('□') + 1));
                    if (dic.ContainsKey(key))
                        dic[key] += value;
                    else
                        dic.Add(key, value);
                }
                StringBuilder sb = new StringBuilder();
                foreach (string s in dic.Keys)
                    sb.AppendFormat("{0}□{1}■", s, dic[s]);
                string result = sb.ToString();
                Console.WriteLine(result);/*
    输出:
    APP001□1□50■APP002□1□80■APP001□2□30■APP002□2□30■
    */
      

  2.   


            public static void Main(string[] args)
            {
                string zz = "APP001□1□20■APP002□1□30■APP001□1□30■APP001□2□30■APP002□1□50■APP002□2□30■";
                Dictionary<string, int> dic = new Dictionary<string, int>();            string[] value= zz.Split('■');//拆分成项
                for (int i = 0; i < value.Length-1; i++)
                {              
                    string k=value[i].Substring(0,value[i].LastIndexOf('□'));
                    int num=Convert.ToInt32(value[i].Substring(value[i].LastIndexOf('□')+1));
                    if(!dic.ContainsKey(k))
                        dic.Add(k,num);
                    else
                        dic[k]+=num;
                }
                string result=string.Empty;
               foreach(string s in dic.Keys)
                   result += s + "□" + dic[s] + "■";
                Console.WriteLine(result);
            }APP001□1□50■APP002□1□80■APP001□2□30■APP002□2□30■
    请按任意键继续. . .
      

  3.   

    定义AppInfo类:
    public class AppInfo
        {
            private string _name;
            private int _id;
            private int _number;        public string Name
            {
                get { return _name; }
                set { _name = value; }
            }        public int Id
            {
                get { return _id; }
                set { _id = value; }
            }        public int Number
            {
                get { return _number; }
                set { _number = value; }
            }        public AppInfo(
                string name,
                int id,
                int number)
            {
                _name = name;
                _id = id;
                _number = number;
            }        public static bool operator ==(AppInfo left, AppInfo right)
            {
                if (object.ReferenceEquals(left, right))
                {
                    return true;
                }
                else if (object.ReferenceEquals(left, null)
                    || object.ReferenceEquals(right, null))
                {
                    return false;
                }
                else
                {
                    return (left._name == right.Name) && (left._id == right._id);
                }
            }        public static bool operator !=(AppInfo left, AppInfo right)
            {
                return !(left == right);
            }        public override bool Equals(object obj)
            {
                return this == (obj as AppInfo);
            }        public override string ToString()
            {
                return string.Format("{0}□{1}□{2}■", _name, _id, _number);
            }
        }方法:
    string GetString(string oldValue)
            {
                List<AppInfo> list = new List<AppInfo>();
                string[] oldValues = oldValue.Split(
                    new char[] { '■' },
                    StringSplitOptions.RemoveEmptyEntries);
                foreach (string value in oldValues)
                {
                    string[] values = value.Split(
                        new char[] { '□' },
                        StringSplitOptions.RemoveEmptyEntries);
                    if (values.Length == 3)
                    {
                        AppInfo info = new AppInfo(
                            values[0],
                            int.Parse(values[1]),
                            int.Parse(values[2]));
                        AppInfo hasInfo = list.Find(new Predicate<AppInfo>(
                            delegate(AppInfo appInfo)
                            {
                                if (appInfo == info)
                                {
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                            }));
                        if (hasInfo == null)
                        {
                            list.Add(info);
                        }
                        else
                        {
                            hasInfo.Number += info.Number;
                        }
                    }
                }            StringBuilder sb = new StringBuilder();
                foreach (AppInfo info in list)
                {
                    sb.Append(info.ToString());
                }
                return sb.ToString();
            }
    用法:
    MessageBox.Show(
                    GetString(
                    "APP001□1□20■APP002□1□30■APP001□1□30■APP001□2□30■APP002□1□50■APP002□2□30■"));
      

  4.   

    Dictionary<string, int> dic = new Dictionary<string, int>();
    保存数据,string为key。int为数值和,通过ContainsKey判断是否有相同 key
    还可使用List<T>定义哦实体类,给实体类属性赋值,判断list<T>里是否有相同的实体
    分割使用string dd = "";
    string[] tempArray = Regex.Split(dd,@"■",RegexOptions.IgnoreCase);
    foreach(string i in tempArray)
    {
     Response.Write(i.ToString());
      Response.Wite("<br />");
    }
    string[] tempArray = dd.Split(new char[] { '■', '□' });