using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 字符串的简单练习 { class Program { static void Main(string[] args) { } private static void Test9() { //数组的转置 string[] countrys = { "中", "德", "美", "法", "日" }; Rever(countrys); foreach (var item in countrys) { Console.WriteLine(item); } Console.Read(); } private static void Test8() { //求员工工资文件中,员工的最高工资、最低工资、平均工资 string[] lines = File.ReadAllLines(@"C:UsershomeDesktop1.txt", Encoding.Default);//读取 double[] salays = new double[lines.Length]; //string[] a = new string[lines.Length]; for (int i = 0; i < lines.Length; i++) { //此时 "zhangsan","12" salays[i] = Convert.ToDouble(lines[i].Split(new char[2] { ',', '"' }, StringSplitOptions.RemoveEmptyEntries)[1]); } double sum = 0; double min = salays[0]; double max = salays[0]; for (int i = 0; i < salays.Length; i++) { sum += salays[i]; if (max < salays[i]) { max = salays[i]; } if (min > salays[i]) { min = salays[i]; } } Console.WriteLine("最高工资{0}最低工资{1}平均工资{2}", max, min, sum / salays.Length); Console.Read(); } private static void Test7() { ///“192.168.10.5[port=21,type=ftp]”, ///这个字符串表示IP地址为192.168.10.5的服务器的21端口提供的是ftp服务, ///其中如果“,type=ftp”部分被省略,则默认为http服务。 ///请用程序解析此字符串,然后打印出“IP地址为***的服务器的***端口提供的服务为***” ///line.Contains(“type=”)。192.168.10.5[port=21] string address = "192.168.10.5[port=21,type=ftp]"; //string address = "192.168.10.5[port=8080]"; //1获取IP地址 string ip = address.Substring(0, address.IndexOf("[")); string port = address.Substring(address.IndexOf("=") + 1, address.IndexOf("]") - address.IndexOf("=") - 1); string type = "http"; if (address.Contains("type")) { port = address.Substring(address.IndexOf("=") + 1, address.IndexOf(",") - address.IndexOf("=") - 1); type = address.Substring(address.LastIndexOf("=") + 1, address.IndexOf("]") - address.LastIndexOf("=") - 1); } Console.WriteLine("IP地址为{0}的服务器的{1}端口提供的服务为{2}", ip, port, type); Console.Read(); } private static void Test6() { ///从文件路径中提取出文件名(包含后缀) 。 ///比如从c:aewqqeqewq.txt中提取出b.txt这个文件名出来。 ///以后还会学更简单的方式:“正则表达式”, ///项目中我们用微软提供的:Path.GetFileName();(更简单。) string str = @"c:aewqqeqewq.txt"; int sindex = str.LastIndexOf("\"); string result = str.Substring(sindex + 1); Console.WriteLine(result); Console.Read(); } private static void Test5() { ///练习5:123-456---7---89-----123----2把类似的字符串中重复符号”-”去掉, ///既得到123-456-7-89-123-2. ///split()、StringSplitOptions.RemoveEmptyEntries Join() string str = "123-456---7---89-----123----2"; string[] strs = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); string result = string.Join("-", strs); Console.WriteLine(result); Console.Read(); } private static void Test4() { ///把csv文件中的联系人姓名和电话显示出来。 ///姓名:张三 电话:15001111113 string[] lines = File.ReadAllLines(@"C:UsershomeDesktop1.txt", Encoding.Default);//读取 foreach (string item in lines) { //此时 "","","12" string[] Tele = item.Split(new char[2] { '"', ',' }, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine("姓名:{0} 电话:{1}", Tele[0] + Tele[1], Tele[2]); } Console.Read(); } private static void Test3() { //”2012年12月21日”从日期字符串中把年月日分别取出来,打印到控制台 string str = "2012年2月21日"; int yearIndex = str.IndexOf("年"); int moonIndex = str.IndexOf("月"); int dayIndex = str.IndexOf("日"); string year = str.Substring(0, yearIndex); string moon = str.Substring(yearIndex + 1, moonIndex - yearIndex - 1); string day = str.Substring(moonIndex + 1, dayIndex - moonIndex - 1); Console.WriteLine(year); Console.WriteLine(moon); Console.WriteLine(day); Console.Read(); } private static void Test2() { //接收用户输入的一句英文,将其中的单词以反序输出。 “I love you"→“I evol uoy" string str = "I love you"; //1字符串分割 string[] strs = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (string item in strs) { Console.Write(Reversal(item)); Console.Write(" "); } Console.Read(); } private static void Test1() { ///课上练习1:接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba“. string abc = "abc"; char[] newStrs = Reversal(abc); Console.WriteLine(newStrs); Console.Read(); } //字符反转 private static char[] Reversal(string abc) { char[] strs = abc.ToCharArray(); char[] newStrs = new char[strs.Length]; for (int i = 0; i < strs.Length; i++) { newStrs[i] = strs[strs.Length - i - 1]; } return newStrs; } //数组的反转 private static void Rever(string [] arry) { for (int i = 0; i < arry.Length/2; i++) { string temp = arry[i]; arry[i] = arry[arry.Length-1-i]; arry[arry.Length-1-i] = temp; } } } }