C#字符串所包含的方法

  // split();除去字符串中不需要的字符,如空格等等返回字符串的数组


            string s = "1, 2  3  5  ;  = 8";
            char[] chs = {'=' };
            string []str=s.Split(chs,StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < str.Length; i++)
            {
                Console.Write(str[i]);
            }
            Console.ReadKey();


           // 提示用户输入2008-02-9,你输出的日期为2008年1月2日
            Console.WriteLine("请输入一个日期的格式");
            string input = Console.ReadLine();
            char[] chs = {'-' };
            string []str = input.Split(chs,StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine("{0}年{1}月{2}日",str[0],str[1],str[2]);
            Console.ReadKey();


            //length()方法,获得当前字符串中字符的个数
            string[] s = { "123456"};
            Console.WriteLine(s.Length);


            ToUpper将字符串改成大写的  ToLower改成小写
            Console.WriteLine("请输入科目");
            string input = Console.ReadLine();
            input=input.ToUpper();
            Console.WriteLine("再输一次");
            string input2 = Console.ReadLine();
            input2 = input2.ToUpper();
            if (input == input2)
            {
                Console.WriteLine("同一个");
            }
            else 
            {
                Console.WriteLine("不一样");
            }
            Console.ReadKey();




           // Equals(lesson,stringcomparison.ordinalignorecase)比较两个字符
            Console.WriteLine("请输入科目");
            string input = Console.ReadLine();
            Console.WriteLine("再输一次");
            string input2 = Console.ReadLine();
            if (Equals(input, StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("相同");
            }
            else
            {
                Console.WriteLine("不同");
            }


           // substring 截取字符串,在截取的时候包含要截取的哪个位置
            string s = "abcdefghj";
            s=s.Substring(1, 5);
            Console.WriteLine(s);
            Console.ReadKey();




            //indexof 字符串中第一次出现的字符 返回的是一个int类型  lastindexof最后一次出现的字符
            string s = "1142589";
            int a=s.IndexOf('9');
            Console.WriteLine(a);
            Console.ReadKey();


           //contains判断字符串中是否有指定字符
            string s = "12458684";
            if (s.Contains("6"))
            {
                Console.WriteLine("有");
            }
            else { Console.WriteLine("没有");}
            Console.ReadKey();


           // startwith 判断以,,开始,endwith 以。。。结束
            string s = "12345876";
            if (s.StartsWith("4"))
            {
                Console.WriteLine("正确");
            }
            else 
            {
                Console.WriteLine("false");
            }
            Console.ReadKey();


            //replace将字符串某个字符替换成新的字符
            string s = "国家关键人物老赵";
            if (s.Contains("老赵"))
            {
              s=  s.Replace("老赵", "**");
            }
            Console.WriteLine(s);
            Console.ReadKey();


            //trim 去掉空格  trimstart 去掉前面空格 trimend去掉后面空格
            string s = "  125   125   ";
            s=s.Trim();
            
            Console.WriteLine(s);
            Console.ReadKey();


           // lastindexof 和  substring 连用要求截取"苍老师。waf"
            string s = @"c:\s\s\s\s\s\sd\d\fg\h\苍老师。waf";
            int index=s.LastIndexOf('\\');//从最后的“\”
            s = s.Substring(index);//截取“\”后的内容
           Console.WriteLine(s);
           Console.ReadKey();


           // string.isnullorempty判断一个字符串是否为空
            string s = "123";
            if (string.IsNullOrEmpty(s))
            {
                Console.WriteLine("是的");
               
            }
            else { Console.WriteLine("不是"); }
            Console.ReadKey();




           // string.join()将数组按照指定字符串连接
            string[] s = { "张三","李四"};
           string input=string.Join("\\", s);
           Console.WriteLine(input);
           Console.ReadKey();
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值