课上练习1:接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba"
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
课上练习1namespace 练习01 { class Program { static void Main(string[] args) { Console.WriteLine("请输入字符串"); string str = Console.ReadLine(); string str1=Exchange(str); Console.WriteLine(str1); Console.ReadKey(); } public static string Exchange(string str1) { char[] c1 = str1.ToCharArray(); for (int i = 0; i < c1.Length; i++) { c1[i] = str1[str1.Length - 1 - i]; } str1 = new string(c1); return str1; } } }
课上练习2:接收用户输入的一句英文,将其中的单词以反序输出。 “I love you"→“i evol uoy"
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
课上练习2class Program { static void Main(string[] args) { Console.WriteLine("请输入字符串"); string str=Console.ReadLine(); string [] str1 = str.Split(new char[] {' ','!'}, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < str1.Length; i++) { str1[i] = Exchange(str1[i]); } string str2 = string.Join("", str1); Console.WriteLine(str2); Console.ReadKey(); } public static string Exchange(string str1) { char[] c1 = str1.ToCharArray(); for (int i = 0; i < c1.Length; i++) { c1[i] = str1[str1.Length - 1 - i]; } str1 = new string(c1); return str1; } } }
课上练习3:”2012年12月21日”从日期字符串中把年月日分别取出来,打印到控制台
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
课上练习3方法1: class Program { static void Main(string[] args) { string str = "2012年12月21日"; string [] strs=str.Split(new char []{'年','月','日'},StringSplitOptions.RemoveEmptyEntries); string s = string.Join("", strs); Console.WriteLine(s); Console.ReadKey(); } } 方法2:namespace 练习03 { class Program { static void Main(string[] args) { string str = "2012年12月21日"; int index1 = str.IndexOf("年"); int index2 = str.IndexOf("月"); int index3 = str.IndexOf("日"); string str1 = str.Substring(index1 - 4, 4); string str2 = str.Substring(index2 - 2, 2); string str3 = str.Substring(index3 - 2, 2); string s = string.Format("{0} {1} {2}", str1, str2, str3); Console.WriteLine(s); Console.ReadKey(); } } }
课上练习4:把csv文件中的联系人姓名和电话显示出来。简单模拟csv文件,csv文件就是使用,分割数据的文本,输出:
姓名:张三 电话:15001111113
string[] lines = File.ReadAllLines(“1.csv”,Encoding.Default); //读取文件中的所有行,到数组中。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
课上练习4namespace 练习04 { class Program { static void Main(string[] args) { string[] lines = File.ReadAllLines(@"F:\传智学习记录\上课视频资料\20120629第六天_字符串_集合\20120629第六天_字符串_Source\Source\电话.csv",Encoding.Default); for (int i = 0; i < lines.Length; i++) { string [] str=lines[i].Split(','); string res=string.Format("姓名:{0} \t 手机:{1}\t",str); Console.WriteLine(res); } Console.ReadKey(); } } }