字符串的练习。

  1     class Program
  2     {
  3         static void Main(string[] args)
  4         {
  5             /*课上练习1:接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba"*/
  6             //string str = "abcdefg";//gfedcba    
  7             //char[] chs = str.ToCharArray();           
  8             //for (int i = 0; i < chs.Length / 2; i++)                                                  
  9             //{
 10             //    char temp = chs[i];
 11             //    chs[i] = chs[chs.Length - 1 - i];
 12             //    chs[chs.Length - 1 - i] = temp;
 13             //}
 14             //str = new string(chs);
 15             //Console.WriteLine(str);
 16             //Console.ReadKey();
 17 
 18             /*倒叙循环*/
 19             //for (int i = str.Length - 1; i >= 0; i--)
 20             //{
 21             //    Console.Write(str[i]);
 22             //}
 23 
 24             /*"hello c sharp"→"sharp c hello"*/
 25             //string str = "hello c sharp";
 26             //string[] strNew = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
 27             //for (int i = 0; i < strNew.Length / 2; i++)
 28             //{
 29             //    string temp = strNew[i];
 30             //    strNew[i] = strNew[strNew.Length - 1 - i];
 31             //    strNew[strNew.Length - 1 - i] = temp;
 32             //}
 33             ////string.join:将字符串按照指定的分隔符连接
 34             //str = string.Join(" ", strNew);
 35             //Console.WriteLine(str);//sharp c hello
 36             //Console.ReadKey();
 37 
 38 
 39             /*课上练习3:从Email中提取出用户名和域名:abc@163.com。*/
 40             //string email = "285014478@qq.com";
 41             //int index = email.IndexOf('@');
 42             //string userName = email.Substring(0, index);
 43             //string yuMing = email.Substring(index + 1);
 44             //Console.WriteLine(userName);
 45             //Console.WriteLine(yuMing);
 46             //Console.ReadKey();
 47 
 48 
 49             /*课上练习4:文本文件中存储了多个文章标题、作者,
 50             标题和作者之间用若干空格(数量不定)隔开,每行一个,
 51             标题有的长有的短,输出到控制台的时候最多标题长度10,
 52             如果超过10,则截取长度8的子串并且最后添加“...”,加一个竖线后输出作者的名字。*/
 53             //string path = @"C:\Users\666\1.txt";
 54             //string[] contents = File.ReadAllLines(path, Encoding.Default);
 55             //for (int i = 0; i < contents.Length; i++)
 56             //{
 57             //    string[] strNew = contents[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
 58             //    Console.WriteLine((strNew[0].Length > 10 ? strNew[0].Substring(0, 8) + "......" : strNew[0]) + "|" + strNew[1]);
 59             //}
 60             //Console.ReadKey();
 61 
 62 
 63 
 64             /*让用户输入一句话,找出所有e的位置*/
 65             string str = "e、咳嗽 e 咳 咳  咳嗽";
 66             //for (int i = 0; i < str.Length; i++)
 67             //{
 68             //    if (str[i] == 'e')
 69             //    {
 70             //        Console.WriteLine(i);
 71             //    }
 72             //}
 73             //Console.ReadKey();
 74 
 75             //int index = str.IndexOf('e');
 76             //Console.WriteLine("第1次出现e的位置是{0}", index);
 77             ////循环体:从上一次出现e的位置加1的位置找下一次e出现的位置
 78             ////循环条件:index!=-1
 79             //int count = 1;//用来记录e出现的次数
 80             //while (index != -1)
 81             //{
 82             //    count++;
 83             //    index = str.IndexOf('e', index + 1);
 84             //    if (index == -1)
 85             //    {
 86             //        break;
 87             //    }
 88             //    Console.WriteLine("第{0}次出现e的位置是{1}", count, index);
 89             //}
 90             //Console.ReadKey();
 91 
 92 
 93 
 94             //用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:老牛很邪恶,输出后变成老牛很**;
 95             //string str = "老牛很邪恶";
 96             //if (str.Contains("邪恶"))
 97             //{
 98             //    str = str.Replace("邪恶", "**");
 99             //}
100             //Console.WriteLine(str);
101             //Console.ReadKey();
102 
103             //把{“诸葛亮”,”鸟叔”,”卡卡西”,”卡哇伊”}变成诸葛亮|鸟叔|卡卡西|卡哇伊,然后再把|切割掉
104             //  string[] names = { "诸葛亮", "鸟叔", "卡卡西", "卡哇伊" };
105             //  string str = string.Join("|", names);
106             //  string[] strNew = str.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
107             ////  Console.WriteLine(str);
108             //  Console.ReadKey();
109         }
110     }

 

posted @ 2020-09-21 11:37  技术不够脸来凑  阅读(178)  评论(0)    收藏  举报