字符串的各种方法
stringbuilder举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace _05StringBuilder 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 StringBuilder sb = new StringBuilder(); 15 string str = "0"; 16 //创建了一个计时器,用来记录程序运行的时间 17 //00:00:00.0422166 18 Stopwatch sw = new Stopwatch(); 19 sw.Start();//开始计时 20 for (int i = 0; i < 100000; i++) 21 { 22 //str += i; 23 sb.Append(i); 24 } 25 sw.Stop();//结束计时 26 Console.WriteLine(sb.ToString()); 27 Console.WriteLine(sw.Elapsed); 28 Console.ReadKey(); 29 } 30 } 31 }
length举例:随机输入你心中想到的一个名字,然后输出它的字符串长度 length:可以得字符串的长度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Console.WriteLine("请输入你心中想的那个人的名字"); 2 string name = Console.ReadLine(); 3 Console.WriteLine("你心中想的人的名字的长度是{0}", name.Length); 4 Console.ReadKey();
toupper,tolower,equals举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Console.WriteLine("请输入你喜欢的课程"); 2 string lessonOne = Console.ReadLine(); 3 //将字符串转换成大写 4 // lessonOne = lessonOne.ToUpper(); 5 //将字符串转换成小写形式 6 // lessonOne = lessonOne.ToLower(); 7 Console.WriteLine("请输入你喜欢的课程"); 8 string lessonTwo = Console.ReadLine(); 9 // lessonTwo = lessonTwo.ToUpper(); 10 // lessonTwo = lessonTwo.ToLower(); 11 if (lessonOne.Equals(lessonTwo, StringComparison.OrdinalIgnoreCase)) 12 { 13 Console.WriteLine("你们俩喜欢的课程相同"); 14 } 15 else 16 { 17 Console.WriteLine("你们俩喜欢的课程不同"); 18 } 19 Console.ReadKey();
split举例:从日期字符串("2008-08-08")中分析出年、月、日;2008年08月08日。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string s = "2008-08-08"; 2 // char[] chs = { '-' }; 3 string[] date = s.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); 4 Console.WriteLine("{0}年{1}月{2}日", date[0], date[1], date[2]); 5 Console.ReadKey();
contains,replace举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string str = "国家关键人物老赵"; 2 if (str.Contains("老赵")) 3 { 4 str = str.Replace("老赵", "**"); 5 } 6 Console.WriteLine(str); 7 Console.ReadKey();
substring举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string str = "今天天气好晴朗,处处好风光"; 2 str = str.Substring(1, 2); 3 Console.WriteLine(str); 4 Console.ReadKey();
endswith举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string str = "今天天气好晴朗,处处好风光"; 2 if (str.EndsWith("风")) 3 { 4 Console.WriteLine("是的"); 5 } 6 else 7 { 8 Console.WriteLine("不是的"); 9 } 10 Console.ReadKey();
indexof,lastindexof举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string str = "今天天天气好晴朗,天天处天好风光"; 2 int index = str.IndexOf('哈', 2); 3 Console.WriteLine(index); 4 Console.ReadKey(); 5 6 7 //string str = "今天天气好晴朗,处处好风光"; 8 //int index = str.LastIndexOf('天'); 9 //Console.WriteLine(index); 10 //Console.ReadKey(); 11 12 13 ////LastIndexOf Substring 14 //string path = @"c:\a\b\c苍\d\e苍\f\g\\fd\fd\fdf\d\vfd\苍老师苍.wav"; 15 //int index = path.LastIndexOf("\\"); 16 //path = path.Substring(index + 1); 17 //Console.WriteLine(path); 18 //Console.ReadKey();
trim,trimstart,trimend举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string str = " hahahah "; 2 str = str.Trim(); 3 //str = str.TrimStart(); 4 //str = str.TrimEnd(); 5 Console.Write(str); 6 Console.ReadKey();
string.IsNullOrEmpty,string.Join举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string str = "fdsfdsfds"; 2 if (string.IsNullOrEmpty(str)) 3 { 4 Console.WriteLine("是的"); 5 } 6 else 7 { 8 Console.WriteLine("不是"); 9 } 10 string[] names = { "张三", "李四", "王五", "赵六", "田七" }; 11 //张三|李四|王五|赵六|田七 12 string strNew = string.Join("|", "张三", "李四", "王五", "赵六", "田七"); 13 Console.WriteLine(strNew); 14 Console.ReadKey();