C#第六天

字符串的处理练习:

课上练习1:接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba"

方法1:

1 string str = "abcdefg";
2             for (int i = str.Length - 1; i >= 0; i--)
3             {
4                 //倒叙循环
5                 Console.WriteLine(str[i]);
6             }
7             Console.ReadKey();
View Code

方法2:

 1     string str = "abcdefg";
 2                 char[] chs = str.ToCharArray();
 3                 for (int i = 0; i < chs.Length / 2; i++)
 4                 {
 5                     //数组反转
 6                     char temp = chs[i];
 7                     chs[i] = chs[chs.Length - 1 - i];
 8                     chs[chs.Length - 1 - i] = temp;
 9                 }
10                 str = new string(chs);
11                 Console.WriteLine(str);
12                 Console.ReadKey();
View Code

课上练习2:接收用户输入的一句英文,将其中的单词以反序输出。"hello c sharp"→"sharp c hello"

 1 string str = "hello c sharp";
 2             string[] strNew = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
 3             for (int i = 0; i < strNew.Length / 2; i++)
 4             {
 5                 string temp = strNew[i];
 6                 strNew[i] = strNew[strNew.Length - 1 - i];
 7                 strNew[strNew.Length - 1 - i] = temp;
 8             }
 9             //将字符串数组按照指定的分隔符连接,返回一个字符串
10             string s = string.Join(" ", strNew);
11             Console.WriteLine(s);
12             Console.ReadKey();
View Code

课上练习3:从Email中提取出用户名和域名:abc@163.com。

1     string email = "abc@163.com";
2             int index = email.IndexOf('@');
3             string userName = email.Substring(0,index);
4             string yuMing = email.Substring(index + 1);
5             Console.WriteLine(userName);
6             Console.WriteLine(yuMing);
7             Console.ReadKey();
View Code

课上练习4:文本文件中存储了多个文章标题、作者,标题和作者之间用若干空格(数量不定)隔开,每行一个,标题有的长有的短,输出到控制台的时候最多标题长度10,如果超过10,则截取长度8的子串并且最后添加“...”,加一个竖线后输出作者的名字。

 1 //文本存储路径为 C:\Users\Administrator\Desktop\操作文本.txt";
 2             //文本内容为:
 3             //平凡的世界   路遥
 4             //坏蛋是怎样炼成的 六道
 5             //遮天             辰东
 6             //C#程序员必读的一些书籍 逗逼秀
 7             string path = @"C:\Users\Administrator\Desktop\操作文本.txt";
 8             string[] contents = File.ReadAllLines(path, Encoding.Default);
 9             for (int i = 0; i < contents.Length; i++)
10             {
11                 string[] strNew = contents[i].Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
12                 Console.WriteLine((strNew[0].Length>6?strNew[0].Substring(0,6)+"......":strNew[0])+"|"+strNew[1]);
13             }
14             Console.ReadKey();
View Code

 练习5:让用户输入一句话,找出所有e的位置

方法1:

1             string str = "adjdslhgenleeklebeiugyveegjbeejkke";
2             for (int i = 0; i < str.Length; i++)
3             {
4                 if (str[i] == 'e')
5                 {
6                     Console.WriteLine(i);
7                 }
8             }
9             Console.ReadKey();
View Code

方法2:

 1             string str = "adjdslhgenleeklebeiugyveegjbeejkke";
 2             int index = str.IndexOf('e');
 3             Console.WriteLine("第一次出现的位置是{0}", index);
 4             //从上一次出现e的位置加1找下一次e出现的位置
 5             //循环条件index!=-1
 6             int count = 1;
 7             while (index != -1)
 8             {
 9                 count++;
10                 index = str.IndexOf('e', index + 1);
11                 if (index == -1)
12                 {
13                     break;
14                 }
15                 Console.WriteLine("第{0}次出现的位置是{1}", count, index);
16             }
17             Console.ReadKey();
View Code

 

posted @ 2015-11-03 14:07  菜鸟,你好  阅读(372)  评论(0编辑  收藏  举报