c# string的常用方法
C#中的字符串类型string是常用的数据类型之一,它用于表示任意长度的Unicode字符序列。下面列举了一些C#中常用字符串的用法:
1.字符串连接
使用"+"符号或者string.Concat方法来连接多个字符串:
string str1 = "Hello";
string str2 = "World";
string str3 = str1 + " " + str2;
string str4 = string.Concat(str1, " ", str2);
2.符串格式化
string str = string.Format("My name is {0} and I am {1} years old.", "Tom", 30);
3.字符串分割
string str = "Hello,World";
string[] strArray = str.Split(',');
4.字符串替换
使用string.Replace方法将字符串中的指定子串替换为新的字符串:
string str = "Hello,World";
string newStr = str.Replace("World", "C#");
5.截取
string str = "Hello,World";
string subStr = str.Substring(0, 5); // 提取前五个字符:Hello
6.获取字符串的长度
string str = "Hello,World";
int len = str.Length; // 获取字符串长度:12
7.判断字符串是否包含指定的子串
使用string.Contains方法判断一个字符串是否包含指定的子串:
string str = "Hello,World";
bool result = str.Contains("Hello"); // 判断字符串是否包含"Hello"子串
8.将字符串转换成大写或小写形式
使用string.ToUpper和string.ToLower方法将字符串转换成大写或小写形式:
string str = "Hello,World";
string upperStr = str.ToUpper(); // 转换成全大写形式
string lowerStr = str.ToLower(); // 转换成全小写形式
9.字符串插值
使用美元符号$和一对大括号{}将表达式(变量、函数调用等)插入到字符串中
string name = "Tom";
int age = 30;
string str = $"My name is {name} and I am {age} years old.";
10.对齐字符串
使用插值表达式中的格式控制符实现字符串的对齐:
int price = 123456;
string str = $"Price: {price,15:C}";
// 输出:Price: $123,456.00
11.插入换行符 使用“\n”插入换行符:
string str = "Hello,\nWorld!";
12.插入制表符 使用“\t”插入制表符:
string str = "Name:\tTom\nAge:\t30";
13.字符串复制
string str = $"{"*", 10}"; // 输出:**********
14.表达式中嵌套括号
如果需要在插值表达式中嵌套大括号,可以使用“{{”和“}}”转义:
string str = $"{{My name is {name}}}";
15.字符串比较
使用string.Compare方法比较两个字符串是否相等:
string str1 = "Hello";
string str2 = "hello";
int result = string.Compare(str1, str2, true);
// result = 0 表示相等
16.删除字符串中指定的字符
string str = "Hello, World!";
string newStr = str.Remove(5, 1); // 删除字符","
17.插入字符串
string str = "Hello, World!";
string newStr = str.Insert(5, "Tom "); // 在“Hello”后插入新的字符串“Tom ”
18.使用string.Join方法将字符串数组转换成一个字符串
string[] words = { "Hello", "World" };
string str = string.Join(" ", words); // 将单词用空格拼接成字符串
19.使用string.Trim方法删除字符串头尾空白字符
string str = " Hello, World! ";
string newStr = str.Trim(); // 删除空白字符,返回"Hello, World!"
20.编码转换
string str = "你好,世界!";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str); // 将字符串编码为UTF-8字节数组
string newStr = System.Text.Encoding.ASCII.GetString(bytes); // 将UTF-8字节数组转换为ASCII编码的字符串
21.字符串查找
使用string.IndexOf方法查找指定字符或子串的位置:
string str = "Hello, World!";
int pos = str.IndexOf(','); // 返回","字符的位置
int pos2 = str.IndexOf("World"); // 返回"World"子串的位置
22.字符串验证
使用string.IsNullOrEmpty方法判断字符串是否为空或null:
string str = null;
bool result = string.IsNullOrEmpty(str); // 返回true
23.整数格式化字符串
C# 9.0中string.Format方法支持新的整数格式化字符串,例如使用“0b”前缀表示二进制格式化输出:
int n = 42;
string str = $"The answer is {n:0b}"; // 输出:"The answer is 101010"
24.空值合并运算符的字符串版本
使用空值合并运算符(??)的字符串版本,用于替换null引用值:
string name = null;
string str = name ?? "unknown"; // 如果name为null,则返回"unknown",否则返回name的值
25.空值条件运算符的字符串版本
使用空值条件运算符(?.)的字符串版本,用于简化null引用值判断:
string name = null;
int length = name?.Length ?? 0; // 如果name为null,则返回0,否则返回name的长度
26.模式匹配的字符串版本
使用模式匹配的字符串版本,可以快速判断字符串是否符合特定的模式:
string s = "hello world";
if (s is { Length: > 5 } str)
{
Console.WriteLine(str); // 只有长度大于5的字符串才会输出
}