字符串

字符串方法

        //1
        //随机输入你心中名字,然后输出他的字符串长度
        //Console.WriteLine("请输入你心中想的那个人的名字");
        //string name = Console.ReadLine();
        //Console.WriteLine("你心中想的人的名字的长度是{0}", name.Length);
        //Console.ReadKey();

        //2
        //Console.WriteLine("请输入你喜欢的课程");
        //string lessonOne = Console.ReadLine();
        //将字符串转换成大写
        //lessonOne = lessonOne.ToUpper();
        //Console.WriteLine("请输入你喜欢的课程");
        //string lessonTwo = Console.ReadLine();
        //将字符串转换成大写
        //lessonTwo = lessonTwo.ToUpper();
        //if (lessonOne.Equals(lessonTwo,StringComparison.OrdinalIgnoreCase))
        //{
        //    Console.WriteLine("你们俩喜欢的课程相同");
        //}
        //else
        //{
        //    Console.WriteLine("你们俩喜欢的课程不同");
        //}
        //Console.ReadKey();

        //3
        //string s = "a b dfd _ + =  ,,, fdf ";
        分割字符串Split
        //char[] chs = {' ','_','+','=',',' };
        //string[] str=s.Split(chs,StringSplitOptions.RemoveEmptyEntries);
        //Console.ReadKey();

        //4
        //练习:从日期字符串(“2008-08-08”)中分析出年、月、日;2008年08月08日。
        //让用户输入一个日期格式如下:2008-01-02,你输出的日期为:2008年1月2日
        //string s = "2008-08-08";
        char[] chs={'_'};
        //string[] date = s.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
        //Console.WriteLine("{0}年{1}月{2}日", date[0], date[1], date[2]);
        //Console.ReadKey();

        //5  str.Replace
        //string str = "国家关键人物老赵";
        //if (str.Contains("老赵"))
        //{
        //    str=str.Replace("老赵", "**");
        //}
        //Console.WriteLine(str);
        //Console.ReadKey();

        //6   Substring  截取字符串
        //string str = "今天天气好晴朗,处处好风光";
        //str = str.Substring(1,2);       //开始截取点    截取个数
        //Console.WriteLine(str);
        //Console.ReadKey();

        //7  StartsWith
        //string str = "今天天气好晴朗,处处好风光";
        //if (str.StartsWith("今天"))
        //{
        //    Console.WriteLine("是的");
        //}
        //else
        //{
        //    Console.WriteLine("不是的");
        //}
        //Console.ReadKey();

        //8  EndsWith
        //string str = "今天天气好晴朗,处处好风光";
        //if (str.EndsWith("风光"))
        //{
        //    Console.WriteLine("是的");
        //}
        //else
        //{
        //    Console.WriteLine("不是的");
        //}
        //Console.ReadKey();

        //9   字符第一次出现的索引
        //string str = "今天天气好晴朗,处处好风光";
        //int index = str.IndexOf('天',1);       //字符串    从第几个开始找
        //Console.WriteLine(index);
        //Console.ReadKey();

        //10  字符最后一次出现的索引
        //string str = "今天天气好晴朗,处处好风光";
        //int index = str.LastIndexOf('天');       //字符串    从第几个开始找
        //Console.WriteLine(index);
        //Console.ReadKey();

        //11  LastIndexOf  Substring
        //string path = @"c:\a\b\c\d苍\e苍\f\g\\td\fd\fdf\d\vdf\苍老师.wav";
        //int index = path.LastIndexOf("\\");
        //path = path.Substring(index+1);
        //Console.WriteLine(path);
        //Console.ReadKey();

        //12  去空字符
        //string str = "     hahaha       ";
        str=str.Trim();
        str = str.TrimStart();
        //str = str.TrimEnd();
        //Console.WriteLine(str);
        //Console.ReadKey();

        //13   判断是不是空
        //string str = "dscsuic";
        //if (string.IsNullOrEmpty(str))
        //{
        //    Console.WriteLine("是的");
        //}
        //else
        //{
        //    Console.WriteLine("不是的");
        //}
        //Console.ReadKey();

        //14  字符串数组加|
        String[] names = { "张三", "李四", "王五", "赵六","田七" };
        //张三|李四|王五|赵六|田七
        //string strNew=string.Join("|", names);
        string strNew = string.Join("|", "张三", "李四", "王五", "赵六", "田七");
        Console.WriteLine(strNew);
        Console.ReadKey();





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

        //历史就是这么回事儿|袁腾飞
        //C#基础之循环结构while、do-while|老赵
        //AV|苍老师
        //坏蛋是怎样炼成的怎样炼成的|六道
        //string path = @"C:\Users\29540\Desktop\1.txt";
        //string[] contents=File.ReadAllLines(path,Encoding.Default);
        //for(int i = 0; i < contents.Length; i++)
        //{
        //    string[] strNew = contents[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        //    Console.WriteLine((strNew[0].Length>10?strNew[0].Substring(0,8)+"......":strNew[0])+ "|" + strNew[1]);
        //}
        //Console.ReadKey();

课堂练习

        //课堂练习1:接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。“abc"-"cba"
        //string str = "abcdefg";
        //char[] chs = str.ToCharArray();
        //for(int i = 0; i < chs.Length / 2; i++)
        //{
        //    char temp = chs[i];
        //    chs[i] = chs[chs.Length - 1 - i];
        //    chs[chs.Length - 1 - i] = temp;
        //}
        //str = new string(chs);
        //Console.WriteLine(str);
        //Console.ReadKey();

        //倒叙循环   错误的做法
        //for(int i = str.Length - 1; i >= 0; i--)
        //{
        //    Console.WriteLine(str[i]);
        //}
        //Console.ReadKey();

        //课堂练习2;"hello c sharp"->"sharp c hello"
        //string.join:将字符串按照指定的分割符连接
        //string str = "hello c sharp";
        //string[] strNew=str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        //for(int i = 0; i < strNew.Length / 2; i++)
        //{
        //    string temp = strNew[i];
        //    strNew[i] = strNew[strNew.Length - 1 - i];
        //    strNew[strNew.Length - 1 - i] = temp;
        //}
        //str = string.Join(" ", strNew);
        //      //for (int i = 0; i < strNew.Length; i++)
        //     //{
        //     //    Console.WriteLine(strNew[i]);
        //      //}
        //Console.WriteLine(str);
        //Console.ReadKey();


        //课堂练习3:从Email中提取出用户名和域名:abc@163.com
        //string email = "285014478@qq.com";
        //int index = email.IndexOf('@');
        //string userName = email.Substring(0, index);
        //string yuMing = email.Substring(index+1);
        //Console.WriteLine(userName);
        //Console.WriteLine(yuMing);
        //Console.ReadKey();

        //课堂练习5:让用户输入一句话,找出所有e的位置
        //string str = "abcdefefefefdfefefd";
        //for(int i = 0; i < str.Length; i++)
        //{
        //    if (str[i] == 'e')
        //    {
        //        Console.WriteLine(i);
        //    }
        //}
        //Console.ReadKey();



        //第一种方法   好方法 必须掌握
        //int index = str.IndexOf('e');
        //Console.WriteLine("第一次出现e的位置是{0}", index);
        循环体:从上一次出现e的位置加1的位置找下一次e出现的位置
        循环条件:index!=-1;
        //int count = 1;
        //while (index != -1)
        //{
        //    count++;
        //    index = str.IndexOf('e', index + 1);
        //    if (index == -1)
        //    {
        //        break;
        //    }
        //    Console.WriteLine("第{0}次出现e的位置是{1}", count, index);
        //}
        //Console.ReadKey();

        //课堂练习6:用户输入一句话,判断这句话有没有邪恶
        //string str = "老牛很邪恶";
        //if (str.Contains("邪恶"))
        //{
        //    str=str.Replace("邪恶", "**");
        //}
        //Console.WriteLine(str);
        //Console.ReadKey();

        //把{“诸葛亮”,“鸟叔”,“卡卡西”,“卡哇伊”}
        string[] names = {"诸葛亮","鸟叔","卡卡西","卡哇伊"};
        string str=string.Join("|", names);
        string[] strNew = str.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        //Console.WriteLine(str);
        Console.ReadKey();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值