一、DateTime 日期時間操作
var now = DateTime.Now;
// 元件存取
int year = now.Year, month = now.Month, day = now.Day;
int hour = now.Hour, minute = now.Minute, second = now.Second, ms = now.Millisecond;
// 標準格式輸出
now.ToString("f"); // 完整日期短時間 (長日期+短時間)
now.ToString("F"); // 完整日期長時間
now.ToString("g"); // 一般日期短時間
now.ToString("G"); // 一般日期長時間
now.ToString("s"); // ISO 8601 可排序 2023-10-15T14:30:45
now.ToString("u"); // 通用可排序 UTC 格式 2023-10-15 14:30:45Z
now.ToString("r"); // RFC1123 HTTP 標頭
now.ToString("y"); // 年月
now.ToString("m"); // 月日
now.ToString("D"); // 長日期
now.ToString("t"); // 短時間
now.ToString("T"); // 長時間
// 日期運算
DateTime future = now.AddDays(100);
DateTime past = now.AddMonths(-3);
TimeSpan diff = future - now;
二、型別轉換
// 字串 → 數值
int i = Int32.Parse("123");
int i2 = Convert.ToInt32("123"); // null 回 0
bool ok = Int32.TryParse("abc", out int result); // 安全轉換
// 數值 → 字串格式化
12345.ToString("N"); // "12,345.00" 千分位
12345.ToString("C"); // "¥12,345.00" 貨幣
12345.ToString("E"); // "1.234500E+004" 科學記號
12345.ToString("F4"); // "12345.0000" 固定小數
12345.ToString("X"); // "3039" 十六進制
12345.ToString("P"); // "1,234,500.00%" 百分比
// 字串長度
string s = "中國";
int charLen = s.Length; // 字元數 (2)
int byteLen = Encoding.Default.GetBytes(s).Length; // 位元組數 (視編碼而定,Big5=4, UTF-8=6)
三、StringBuilder 高效字串組裝
var sb = new StringBuilder();
sb.Append("中華");
sb.Append("人民");
sb.Append("共和國");
string result = sb.ToString(); // "中華人民共和國"
// 進階
sb.Insert(2, "偉大的"); // 指定位置插入
sb.Replace("人民", "全體人民"); // 替換
sb.Remove(0, 2); // 移除前 2 字元
四、字串常用操作
string str = " Hello World ";
str.Trim(); // "Hello World"
str.TrimStart(); // "Hello World "
str.TrimEnd(); // " Hello World"
"Hello".Substring(1, 3); // "ell" (起始索引, 長度)
"Hello".Substring(2); // "llo" (起始索引到結尾)
"a,b,c".Split(','); // ["a","b","c"]
string.Join("|", new[]{"a","b"}); // "a|b"
"Hello".Replace("l", "L"); // "HeLLo"
"Hello".IndexOf("l"); // 2 (首次出現)
"Hello".LastIndexOf("l"); // 3 (最後出現)
"Hello".Contains("ell"); // true
"Hello".StartsWith("He"); // true
"Hello".EndsWith("lo"); // true
// 插入/填充/移除
"中國".Insert(1, "華"); // "中華國"
"5".PadLeft(4, '0'); // "0005"
"5".PadRight(4, ' '); // "5 "
"HelloWorld".Remove(5, 3); // "HelloD" (索引, 長度)
五、編碼與位元組轉換
string text = "中文";
byte[] bytes = Encoding.UTF8.GetBytes(text); // UTF-8 編碼
byte[] bytes2 = Encoding.GetEncoding("GB2312").GetBytes(text); // GB2312
string back = Encoding.UTF8.GetString(bytes); // 解碼
// Base64
string b64 = Convert.ToBase64String(bytes);
byte[] fromB64 = Convert.FromBase64String(b64);
六、Web 常用 (ASP.NET)
// 取得用戶真實 IP (含 Proxy)
string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
?? Request.ServerVariables["REMOTE_ADDR"];
// QueryString 參數
string id = Request.QueryString["id"];
// Session
Session["UserName"] = "Admin";
string name = Session["UserName"]?.ToString();
Session.RemoveAll(); // 清空
// Response
Response.Write("輸出文字");
Response.Redirect("~/Default.aspx");
Response.ContentType = "application/json";
七、XML 操作 (XmlDocument)
var doc = new XmlDocument();
doc.Load("file.xml");
XmlNode node = doc.SelectSingleNode("//book[@id='1']");
XmlElement newEl = doc.CreateElement("chapter");
newEl.SetAttribute("num", "1");
newEl.InnerText = "內容";
node.AppendChild(newEl);
doc.Save("file.xml");
八、字元判斷 (char 靜態方法)
char.IsWhiteSpace("A B", 1); // true (空白)
char.IsDigit('5'); // true
char.IsLetter('中'); // true
char.IsPunctuation('。'); // true
char.IsUpper('A'); // true
(int)'中'; // 20013 (Unicode 碼位)
(char)22269; // '國'