ASP.NET 字符串去重

来源:互联网 发布:出知西安之咸宁翻译 编辑:程序博客网 时间:2024/06/12 22:07
string strPlanReceiver = string.Empty;            string strPlanReceiveRole = string.Empty;            TaskModel model = new TaskModel();            SystemManager syMan = new SystemManager();            model = syMan.GetTaskReceiverInfoByID(TaskID);  //根据TaskID获取任务接收人配置信息            if (model != null)            {                #region 构造接收人、接收人角色字符串,并去重                //构造接收人、接收人角色字符串,用于发送消息                strPlanReceiver += model.PlanReceiver + ",";                strPlanReceiver += model.PlanReceiver0 + ",";                strPlanReceiver += model.PlanReceiver1 + ",";                strPlanReceiver += model.PlanReceiver2 + ",";                strPlanReceiver += model.PlanReceiver3 + ",";                strPlanReceiver = strPlanReceiver.Trim(',');                strPlanReceiveRole += model.PlanReceiveRole + ",";                strPlanReceiveRole += model.PlanReceiveRole0 + ",";                strPlanReceiveRole += model.PlanReceiveRole1 + ",";                strPlanReceiveRole += model.PlanReceiveRole2 + ",";                strPlanReceiveRole += model.PlanReceiveRole3 + ",";                strPlanReceiveRole = strPlanReceiveRole.Trim(',');                //字符串去重                strPlanReceiver = StringUnique(strPlanReceiver);    //接收人字符串去重                strPlanReceiveRole = StringUnique(strPlanReceiveRole);    //接收角色字符串去重                #endregion            }


#region 字符串去重        /// <summary>        /// 字符串去重        /// </summary>        /// <param name="strList"></param>        /// <returns></returns>        private string StringUnique(string strList)        {             //1.把字符串转换成数组            string result = string.Empty;            string[] str_num = strList.Split(',');            //2.定义ArrayList,判断数组中重复字段不添加到 ArrayList对象中            ArrayList al = new ArrayList();            for (int i = 0; i < str_num.Length; i++)            {                //判断是否已经存在                if (al.Contains(str_num[i]) == false)                {                    al.Add(str_num[i]);                }            }            //3.将arrayList转换成数组            str_num = new String[al.Count];            str_num = (string[])al.ToArray(typeof(string));            //3.数组转字符串,并以逗号间隔            result = string.Join(",", str_num);            return result; ;        }        #endregion