• 一些简单有用的方法合集


            /// <summary>
            /// 处理过长的显示字符串,只显示前几个字符,后面加上“...”
            /// 
            /// </summary>
            /// <param name="str">要处理的字符串</param>
            /// <param name="size">需要的长度</param>
            /// <returns></returns>
            public static string GetSeveralChar(string str, int size)
            {
                return Encoding.Default.GetBytes(str).Length > size ? Encoding.Default.GetString(Encoding.Default.GetBytes(str).Take(size).ToArray()).Replace("?", String.Empty) + "..." : str;
            }
    
            /// <summary>
            /// 注册JS
            /// 
            /// </summary>
            /// <param name="page"></param>
            /// <param name="Method"></param>
            public static void RegisterStartupScript(System.Web.UI.Page page, string Method)
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), DateTime.Now.ToString(), "<script type='text/javascript'>" + Method + "</script>");
            }
    
            /// <summary>
            /// 根据数字得到二进制的字符串
            /// 
            /// </summary>
            /// <param name="num"></param>
            /// <returns></returns>
            public static string GetBlobFromNum(int num)
            {
                int len = sizeof(int) * 8;
                StringBuilder str = new StringBuilder();
                int exmple = 1 << len - 1;
                int tmp = num;
                for (int i = 0; i < len; i++)
                {
                    str.Append((tmp & exmple) == 0 ? "0" : "1");
                    tmp <<= 1;
                }
                return str.ToString().Substring(str.ToString().Length - 4);
            }
            /// <summary>
            /// 根据二进制获取数字集合
            /// 
            /// </summary>
            /// <param name="blob"></param>
            /// <returns></returns>
            public static string GetNumArrayByBlob(string blob)
            {
                string NumArray = string.Empty;
                for (int i = 0; i < blob.Length; i++)
                {
                    if (blob.Substring(i, 1) == "1")
                    {
                        NumArray = string.IsNullOrEmpty(NumArray) ? Get2ndPowerNum(blob.Length - i - 1).ToString() : NumArray + "," + Get2ndPowerNum(blob.Length - i - 1).ToString();
                    }
                }
                return NumArray;
            }
            /// <summary>
            /// 计算2的N次方的方法
            /// 
            /// </summary>
            /// <param name="lenth"></param>
            /// <returns></returns>
            public static int Get2ndPowerNum(int lenth)
            {
                return lenth == 0 ? 1 : 2 * Get2ndPowerNum(lenth - 1);
            }
            /// <summary>
            /// 将List转换为字符串
            /// </summary>
            /// <param name="coll"></param>
            /// <returns></returns>
            public static string GetListCollectionByStr(List<string> coll)
            {
                string result = string.Empty;
                for (int i = 0; i < coll.Count; i++)
                {
                    result = string.IsNullOrEmpty(result) ? coll[i] : result + "," + coll[i];
                }
                return result;
    
            }
           /// <summary>
            /// 从数组中随机取出多个不重复的项 
            /// 作者:刘仁和
            /// </summary>
            /// <param name="list"></param>
            /// <param name="num"></param>
            /// <returns></returns>
            public static IList<string> getListItems(IList<string> list, int num)
            {
                //新建一个泛型列表,将传入的列表复制过来,用于运算,而不要直接操作传入的列表;    
    
                //这样写是引用复制,不对啦
                //IList<string> temp_list = list;
    
                //另外这样写也要注意,也不是深度复制
                IList<string> temp_list = new List<string>(list);
    
                //取出的项,保存在此列表
                IList<string> return_list = new List<string>();
    
                //Random random = new Random(unchecked((int)DateTime.Now.Ticks));
                Random random = new Random();
    
                for (int i = 0; i < num; i++)
                {
                    //判断如果列表还有可以取出的项,以防下标越界
                    if (temp_list.Count > 0)
                    {
                        //在列表中产生一个随机索引
                        int arrIndex = random.Next(0, temp_list.Count);
                        //将此随机索引的对应的列表元素值复制出来
                        return_list.Add(temp_list[arrIndex]);
                        //然后删掉此索引的列表项
                        temp_list.RemoveAt(arrIndex);
                    }
                    else
                    {
                        //列表项取完后,退出循环,比如列表本来只有10项,但要求取出20项.
                        break;
                    }
                }
                return return_list;
            }
  • 相关阅读:
    #背包#nssl 1488 上升子序列
    #环#nssl 1487 图
    #分治#JZOJ 4211 送你一颗圣诞树
    #概率,dp#JZOJ 4212 我想大声告诉你
    #并查集#JZOJ 4223 旅游
    #dp#nssl 1478 题
    #对顶堆#nssl 1477 赛
    #线段树,离散#nssl 1476 联
    #折半搜索,状压dp#nssl 1471 Y
    #并查集,线性筛#nssl 1470 X
  • 原文地址:https://www.cnblogs.com/xue632777974/p/2933910.html
Copyright © 2020-2023  润新知