C#笔记
1.c# String 前面不足位数补零的方法:
String.Format("{0:D5}",16);
or
int i=16;
i.ToString("D5");
Result: 00016
------------------------------------
2.C# Math.Ceiling()向上取整,Math.Floor()向下取整
---------------------------------------
3.C#List按两个关键字排序不用orderBy来
1 //类方法 2 class StudentComp : IComparer<Student> 3 { 4 public int Compare(Student x, Student y) 5 { 6 if (x.Age.CompareTo(y.Age) == 0) //年龄相同时按性别降序排列 7 { 8 return -(x.Gender.CompareTo(y.Gender)); 9 } 10 else 11 { 12 return -(x.Age.CompareTo(y.Age));//年龄降序排列 13 } 14 } 15 } 16 17 m_lstStudent.Sort(new StudentComp()); 18 ---------------------------------------------------- 19 //函数方法 20 public int CompareStudent(Student x, Student y) 21 { 22 if (x.Age.CompareTo(y.Age) == 0) //年龄相同时按性别降序排列 23 { 24 return -(x.Gender.CompareTo(y.Gender)); 25 } 26 else 27 { 28 return -(x.Age.CompareTo(y.Age));//年龄降序排列 29 } 30 } 31 32 m_lstStudent.Sort(CompareStudent);
-----------------------------------------------------
4.按长度来给字符串分组
1 /// <summary> 2 /// 字符串分割成几组 3 /// </summary> 4 /// <param name="strSrc">要分割的字符</param> 5 /// <param name="step">每多少个字符就分割一组</param> 6 /// <param name="strSplit">用什麼字符作为分隔符</param> 7 /// <returns></returns> 8 public string XSplitString(string strSrc, int step, string strSplit) 9 { 10 int i = 0; 11 string strDes = ""; 12 while (i < strSrc.Length) 13 { 14 strDes = strDes + strSrc.Substring(i, step) + strSplit; 15 i += step; 16 } 17 return strDes; 18 }