////// 处理过长的显示字符串,只显示前几个字符,后面加上“...” /// /// /// 要处理的字符串 /// 需要的长度 ///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; } /// /// 注册JS /// /// /// /// public static void RegisterStartupScript(System.Web.UI.Page page, string Method) { page.ClientScript.RegisterStartupScript(page.GetType(), DateTime.Now.ToString(), ""); } ////// 根据数字得到二进制的字符串 /// /// /// ///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); } /// /// 根据二进制获取数字集合 /// /// /// ///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; } /// /// 计算2的N次方的方法 /// /// /// ///public static int Get2ndPowerNum(int lenth) { return lenth == 0 ? 1 : 2 * Get2ndPowerNum(lenth - 1); } /// /// 将List转换为字符串 /// /// ///public static string GetListCollectionByStr(List coll) { string result = string.Empty; for (int i = 0; i < coll.Count; i++) { result = string.IsNullOrEmpty(result) ? coll[i] : result + "," + coll[i]; } return result; } /// /// 从数组中随机取出多个不重复的项 /// 作者:刘仁和 /// /// /// ///public static IList getListItems(IList list, int num) { //新建一个泛型列表,将传入的列表复制过来,用于运算,而不要直接操作传入的列表; //这样写是引用复制,不对啦 //IList temp_list = list; //另外这样写也要注意,也不是深度复制 IList temp_list = new List (list); //取出的项,保存在此列表 IList return_list = new List (); //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; }