C#:随记
1.实现vbs里面的DateDiff方法:(计算毫秒,s/m/d/mon/y依此类推)
DateTime startTime = DateTime.Now; DateTime endTime = DateTime.Now; int dateDiff = (int) new System.TimeSpan(endTime.Ticks - startTime.Ticks).TotalMilliseconds;
2.使用File类写/读log:
File.AppendAllText("1.log", "xx" + "\r\n", Encoding.UTF8);
string[] readAll = File.ReadAllLines("1.log"); string str = null; for (int m = 0; m < readAll.Length; m++) { str += readAll[m] + "\r\n"; }
换行:\r\n
用stream实现:
写:
FileStream fs = new FileStream("1.log", FileMode.Append); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("contents" + "\r\n"); 读:
FileStream fs = new FileStream("1.log", FileMode.Open); StreamReader sr = new StreamReader(fs); string strAll = sr.ReadToEnd();
对字符串排序:"1.txt, 10.txt, 3.txt, 2.txt"
期待结果 "1.txt ,2.txt,10.txt, 3.txt
class Program { string[] arr = { "1.txt", "10.txt", "3.txt", "2.txt" }; static void Main(string[] args) { Program p = new Program(); Dictionary<int, string> dic = new Dictionary<int, string>(); for(int i = 0 ;i< p.arr.Length;i++) { int arrInt =Convert.ToInt32(p.arr[i].Split('.')[0]); string arrString = p.arr[i].Split('.')[1]; dic.Add(arrInt, arrString); } var query = dic.OrderBy(d => d.Key); foreach (var q in query) { Console.WriteLine(q.Key.ToString() +"."+ q.Value); } Console.ReadKey(); } }
找出字符串里所有的字母,并排序:
string str = "aiA[]@#.<>?BjckdlemCfDngpho"; char[] tmpStr = str.ToCharArray(); List<char> myList = new List<char>(); for (int i = 0; i < tmpStr.Length; i++)//找字符 { if ((tmpStr[i] >= 'A' && tmpStr[i] <= 'Z') || (tmpStr[i] >= 'a' && tmpStr[i] <= 'z')) { myList.Add(tmpStr[i]); } } myList.Sort(); //排序 for (var j = 0; j < myList.Count;j++ ) { Console.Write( myList[j].ToString()) ; } Console.ReadKey();