C# 字符串常用庫函數總結
🔹 1. 字符串比較
方法 | 說明 | 示例 |
---|
string.Equals() | 比較兩個字符串是否相等(可忽略大小寫) | string.Equals("abc", "ABC", StringComparison.OrdinalIgnoreCase) |
== / != | 判斷兩個字符串是否相等/不等 | "abc" == "abc" |
string.Compare() | 返回兩個字符串的比較結果(-1, 0, 1) | string.Compare("a", "b") // 返回 -1 |
🔹 2. 查找和判斷
方法 | 說明 | 示例 |
---|
Contains() | 是否包含子串 | "hello".Contains("ell") // true |
StartsWith() | 是否以指定子串開頭 | "hello".StartsWith("he") // true |
EndsWith() | 是否以指定子串結尾 | "hello".EndsWith("lo") // true |
IndexOf() | 返回子串首次出現位置 | "hello".IndexOf("l") // 2 |
LastIndexOf() | 返回子串最后一次出現位置 | "hello".LastIndexOf("l") // 3 |
IsNullOrEmpty() | 判斷是否為 null 或空字符串 | string.IsNullOrEmpty(str) |
IsNullOrWhiteSpace() | 判斷是否為 null、空或全是空白字符 | string.IsNullOrWhiteSpace(" ") |
🔹 3. 截取和拆分
方法 | 說明 | 示例 |
---|
Substring(start, [length]) | 從指定位置截取子串 | "hello".Substring(1, 3) // "ell" |
Split() | 拆分字符串為數組 | "a,b,c".Split(',') // ["a","b","c"] |
Join() | 拼接字符串數組 | string.Join("-", new[] {"a", "b"}) // "a-b" |
🔹 4. 替換與移除
方法 | 說明 | 示例 |
---|
Replace() | 替換子串 | "abcabc".Replace("a", "x") // "xbcxbc" |
Remove() | 刪除指定位置的子串 | "hello".Remove(2, 2) // "heo" |
Trim() | 去除前后空白字符 | " hello ".Trim() // "hello" |
TrimStart() / TrimEnd() | 去除前/后空白字符 | " abc ".TrimStart() // "abc " |
🔹 5. 大小寫轉換
方法 | 說明 | 示例 |
---|
ToLower() | 轉小寫 | "Hello".ToLower() // "hello" |
ToUpper() | 轉大寫 | "Hello".ToUpper() // "HELLO" |
🔹 6. 格式化字符串
方法 | 說明 | 示例 |
---|
string.Format() | 按格式插入變量 | string.Format("Hello {0}", "World") // "Hello World" |
插值字符串($) | 更現代的方式 | $"Hello {name}" |
ToString("格式") | 對數字、日期等格式化 | price.ToString("C") // 顯示貨幣 |
🔹 7. 字符串構建推薦:StringBuilder
類 | 用途 | 示例 |
---|
System.Text.StringBuilder | 拼接大量字符串時性能更好 | |
var stringbuilder = new StringBuilder();
stringbuilder.Append("Hello ");
stringbuilder.Append("World");
string result = stringbuilder.ToString();
🔹 8. 正則表達式(高級查找與替換)
using System.Text.RegularExpressions;
Regex.IsMatch("abc123", @"\d") // true
Regex.Replace("abc123", @"\d", "#") // "abc###"using System.Text.RegularExpressions;
Regex.IsMatch("abc123", @"\d") // true
Regex.Replace("abc123", @"\d", "#") // "abc###"
📘 C# 正則表達式(Regex)詳解
🔹 什么是正則表達式?
正則表達式是一種 用于匹配字符串中某種文本模式的工具,在文本查找、替換、驗證等操作中非常強大。
C# 提供了 System.Text.RegularExpressions
命名空間來支持正則表達式功能。
🔹 基本使用
using System.Text.RegularExpressions;
bool isMatch = Regex.IsMatch("abc123", @"\d");
Match match = Regex.Match("abc123", @"\d+");
MatchCollection matches = Regex.Matches("abc123def456", @"\d+");
string result = Regex.Replace("abc123", @"\d", "#");
🔹 常用元字符(語法規則)
字符 | 含義 | 示例 | 匹配內容 |
---|
. | 任意一個字符 | a.b | 匹配 “acb”, “a1b”,不匹配 “ab” |
\d | 數字(0-9) | \d+ | 匹配 “123”, “456” |
\D | 非數字 | \D+ | 匹配 “abc”, “$%” |
\w | 單詞字符(字母數字下劃線) | \w+ | 匹配 “abc123_” |
\W | 非單詞字符 | \W+ | 匹配 “@#$”, 空格等 |
\s | 空白字符(空格、\t、\n) | \s+ | 匹配空格、Tab、換行 |
\S | 非空白字符 | \S+ | 匹配非空格內容 |
^ | 行的開頭 | ^abc | 匹配以 “abc” 開頭的行 |
$ | 行的結尾 | abc$ | 匹配以 “abc” 結尾的行 |
[...] | 字符集合 | [abc] | 匹配 “a”、“b” 或 “c” |
[^...] | 非字符集合 | [^0-9] | 匹配非數字字符 |
🔹 數量詞(重復匹配)
符號 | 含義 | 示例 | 匹配內容 |
---|
* | 匹配前一個字符 0 次或多次 | a* | 匹配 “”, “a”, “aa”, “aaa” |
+ | 匹配前一個字符 1 次或多次 | a+ | 匹配 “a”, “aa”, “aaa” |
? | 匹配前一個字符 0 次或 1 次 | a? | 匹配 “”, “a” |
{n} | 恰好 n 次 | a{3} | 匹配 “aaa” |
{n,} | 至少 n 次 | a{2,} | 匹配 “aa”, “aaa”, “aaaa” |
{n,m} | n 到 m 次之間 | a{2,4} | 匹配 “aa”, “aaa”, “aaaa” |
🔹 分組與捕獲
語法 | 說明 | 示例 |
---|
(abc) | 捕獲組,匹配 abc | (abc){2} → 匹配 “abcabc” |
(?:abc) | 非捕獲組 | 不保存該分組內容 |
(?<name>abc) | 命名捕獲組 | 可通過名稱訪問 |
\1 , \2 … | 引用之前的分組 | (.)\1 匹配兩個相同字符 |
🔹 常見匹配示例
目標 | 正則表達式 | 示例匹配 |
---|
數字 | ^\d+$ | “123” |
整數(含負號) | ^-?\d+$ | “-456” |
浮點數 | ^-?\d+(\.\d+)?$ | “3.14”, “-2” |
郵箱地址 | ^\w+@\w+\.\w+$ | “test@mail.com” |
手機號碼(中國) | ^1[3-9]\d{9}$ | “13812345678” |
日期(YYYY-MM-DD) | ^\d{4}-\d{2}-\d{2}$ | “2025-07-09” |
🔹 C# 中 Regex 常用 API 匯總
方法 | 說明 |
---|
Regex.IsMatch(input, pattern) | 判斷字符串是否匹配正則 |
Regex.Match(input, pattern) | 返回第一個匹配項 |
Regex.Matches(input, pattern) | 返回所有匹配項 |
Regex.Replace(input, pattern, replacement) | 替換匹配內容 |
Regex.Split(input, pattern) | 按模式拆分字符串 |