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


    .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 "";
            }
  • 相关阅读:
    Nginx日志定时切割脚本
    阿里大于短信接口
    阿里云Linux系统挂载数据盘
    阿里云 OSS+CDN
    值得一学的几条谷歌搜索技巧
    【转】makefile语法规则
    【转】GCC使用简介
    网络编程中常见地址结构与转换(IPv4/IPv6)
    【转】adns解析库——域名解析实例(C++、linux)
    【转】什么是自动化测试
  • 原文地址:https://www.cnblogs.com/amusement1992/p/13495897.html
Copyright © 2020-2023  润新知