C# 字符串处理方法和用法

在 C# 中,字符串处理是非常常见的任务,提供了多种方法来创建、修改和操作字符串。以下是一些常用的字符串处理方法及其用法:

1. 创建字符串

 

csharp

string str1 = "Hello, World!"; string str2 = new string('a', 5); // "aaaaa"

2. 字符串连接

 

csharp

string str1 = "Hello"; string str2 = "World"; string result = str1 + " " + str2; // "Hello World" // 使用 StringBuilder 进行高效连接 StringBuilder sb = new StringBuilder(); sb.Append("Hello"); sb.Append(" "); sb.Append("World"); string result = sb.ToString(); // "Hello World"

3. 字符串长度

 

csharp

string str = "Hello, World!"; int length = str.Length; // 13

4. 字符串比较

 

csharp

string str1 = "Hello"; string str2 = "hello"; bool areEqual = string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase); // true bool areEqualIgnoreCase = str1.Equals(str2, StringComparison.OrdinalIgnoreCase); // true

5. 字符串查找

 

csharp

string str = "Hello, World!"; int index = str.IndexOf("World"); // 7 int lastIndexOf = str.LastIndexOf("o"); // 8

6. 字符串替换

 

csharp

string str = "Hello, World!"; string result = str.Replace("World", "C#"); // "Hello, C#"

7. 字符串分割

 

csharp

string str = "apple,banana,cherry"; string[] parts = str.Split(','); // ["apple", "banana", "cherry"]

8. 字符串截取

 

csharp

string str = "Hello, World!"; string subStr = str.Substring(7, 5); // "World"

9. 字符串格式化

 

csharp

string name = "Alice"; int age = 30; string result = string.Format("Name: {0}, Age: {1}", name, age); // "Name: Alice, Age: 30" // 使用插值字符串(C# 6.0 及以上版本) string result = $"Name: {name}, Age: {age}"; // "Name: Alice, Age: 30"

10. 字符串转换

 

csharp

string str = "hello, world!"; string upperCase = str.ToUpper(); // "HELLO, WORLD!" string lowerCase = str.ToLower(); // "hello, world!" // 转换为字符数组 char[] charArray = str.ToCharArray(); // 转换为数字 int number = int.Parse("123"); // 123 int.TryParse("123", out int parsedNumber); // parsedNumber = 123

11. 字符串修剪

 

csharp

string str = " Hello, World! "; string trimmed = str.Trim(); // "Hello, World!" string trimmedStart = str.TrimStart(); // "Hello, World! " string trimmedEnd = str.TrimEnd(); // " Hello, World!"

12. 字符串判断

 

csharp

string str = "Hello, World!"; bool contains = str.Contains("World"); // true bool startsWith = str.StartsWith("Hello"); // true bool endsWith = str.EndsWith("World!"); // true

13. 字符串编码和解码

 

csharp

string str = "Hello, World!"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str); string decoded = System.Text.Encoding.UTF8.GetString(bytes);

14. 字符串拼接

 

csharp

List<string> items = new List<string> { "apple", "banana", "cherry" }; string result = string.Join(", ", items); // "apple, banana, cherry"

15. 字符串反转

 

csharp

string str = "Hello, World!"; string reversed = new string(str.Reverse().ToArray()); // "!dlroW ,olleH"

16. 字符串去重

 

csharp

string str = "Hello, World!"; string uniqueChars = new string(str.Distinct().ToArray()); // "Helo, Wrld!"

17. 字符串排序

 

csharp

string str = "dcba"; string sorted = new string(str.OrderBy(c => c).ToArray()); // "abcd"

18. 字符串填充

 

csharp

string str = "Hello"; string paddedLeft = str.PadLeft(10, '*'); // "*****Hello" string paddedRight = str.PadRight(10, '*'); // "Hello*****"

19. 字符串插入

 

csharp

string str = "Hello, World!"; string result = str.Insert(7, "Beautiful "); // "Hello, Beautiful World!"

20. 字符串删除

 

csharp

string str = "Hello, World!"; string result = str.Remove(7, 7); // "Hello, "

这些方法涵盖了大多数常见的字符串处理需求。通过组合使用这些方法,可以实现更复杂的字符串操作。希望这些示例对你有所帮助!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路飞VS草帽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值