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) | 按模式拆分字符串 |