首页 > Web开发 > 详细

.net工具类——字符串(List与字符串的转换)

时间:2020-08-13 14:15:43      阅读:92      评论:0      收藏:0      [点我收藏+]

.net工具类——字符串(List与字符串的转换)

  1. strToList:字符串转为List
  2. ListToStr:List转为字符串
  3. ListToIn:List转为sql的in(‘‘,‘‘)

 

        /// <summary>
        /// 把idList转换成List
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static List<string> strToList(string idList)
        {
            List<string> list = new List<string>();
            if (!string.IsNullOrEmpty(idList))
            {
                list = idList.Split(new char[] { , }, StringSplitOptions.RemoveEmptyEntries).Select(p => p).ToList();
            }
            return list;
        }

        /// <summary>
        /// 把idList转换成List
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static List<int?> strToList_int(string idList)
        {
            List<int?> list = new List<int?>();
            if (!string.IsNullOrEmpty(idList))
            {
                list = idList.Split(new char[] { , }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.StrToInt()).ToList();
            }
            return list;
        }

        /// <summary>
        /// 把idList转换成List
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static List<Guid> strToList_Guid(string idList)
        {
            List<Guid> list = new List<Guid>();
            if (!string.IsNullOrEmpty(idList))
            {
                list = idList.Split(new char[] { , }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.StrToGuid() ?? Guid.Empty).ToList();
            }
            return list;
        }

        /// <summary>
        /// 把List转换成idList
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToStr(List<int?> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = string.Join(",", list.Where(d => d.HasValue));
                return str;
            }
            return null;
        }

        /// <summary>
        /// 把List转换成idList
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToStr(List<Guid> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = string.Join(",", list);
                return str;
            }
            return null;
        }

        /// <summary>
        /// 把List转换成idList
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToStr(List<string> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = string.Join(",", list);
                return str;
            }
            return null;
        }

        /// <summary>
        /// 把List转换成idList
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToStr(HashSet<string> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = string.Join(",", list);
                return str;
            }
            return null;
        }

        /// <summary>
        /// 把List转换成Sql的in(‘‘,‘‘)
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToIn(List<string> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = "(";
                foreach (var item in list)
                {
                    str += string.Format("‘{0}‘,", item);
                }
                str = str.Substring(0, str.Length - 1);
                str += ")";
                return str;
            }

            return "";
        }

        /// <summary>
        /// 把List转换成Sql的in(‘‘,‘‘)
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToIn(List<Guid> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = "(";
                foreach (var item in list)
                {
                    str += string.Format("‘{0}‘,", item);
                }
                str = str.Substring(0, str.Length - 1);
                str += ")";
                return str;
            }

            return "";
        }

 

.net工具类——字符串(List与字符串的转换)

原文:https://www.cnblogs.com/amusement1992/p/13495897.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!