zoukankan      html  css  js  c++  java
  • 课堂笔记-------字符串类型string------练习

    字符串类型

      一、string

        //打出s.时就会出现一堆的方框,要找不带箭头的(不带箭头的是我们现在可以用的到的),不要找带箭头的(带箭头的是扩展,现在还用不到)
        //不带箭头的都是对s的操作(动作和行为),就好比s是人有很多的动作和行为。
        //只要是动作和行为;不带箭头;的要加小括号()

        1、Trim() - 去头尾的空格,中间的空格不能去。

           例子:

    1  static void Main(string[] args)  //字符串类型
    2         {
    3             string s = "  Hello World  ";  //左边右边都有空格  "  Hello World  "
    4             Console.WriteLine(s.Trim() + "aaa");                  //Trim() -去左边右边的空格的 。 去头尾的空格,中间的空格不能去。  
    5             
    6           }

    显示的结果

        2、ToUpper() - 把字符串的字母全变成大写。

       例子:

    1 static void Main(string[] args)
    2         {
    3             string s = "Hello World";     
    4             Console .WriteLine (s.ToUpper());       //ToUpper() - 把字符串的字母全变成大写。    

    显示的结果:

          ToLower() - 把字符串的字母全变成小写。

        例子:

    1 static void Main(string[] args)
    2         {
    3             string s = "Hello World";
    4 
    5             Console.WriteLine(s.ToLower());   //ToLower() - 把字符串的字母全变成小写。
    6         }

    显示的结果:

        3、StartsWith("字符串") - 是否以“子串”开头,是-true

          例子:

    1 static void Main(string[] args)
    2         {
    3             string s = "Hello World";
    4             Console.WriteLine(s.StartsWith("He"));   //StartsWith("字符串") - 是否以“子串”开头,是-true
    5 
    6         }

    显示的结果:

        EndsWith("子串") - 是否以“子串”结尾,是-true

    1 static void Main(string[] args)
    2         {
    3             string s = "Hello World";
    4             Console.WriteLine(s.EndsWith("ld"));//EndsWith("子串") - 是否以“子串”结尾,是-true
    5            
    6         }

    显示的结果:

        Contains("子串") - 是否包含“子串”,是-true

    1  static void Main(string[] args)
    2         {
    3 
    4             string s = "Hollo World";
    5             Console.WriteLine(s.Contains("o"));   //Contains("子串") - 是否包含“子串”,是-true
    6             //显示的结果是true
    7        }

    显示的结果:

        Substring(起始位置,长度) - 从大字符串中,截取小的子串出来。

    1 static void Main(string[] args)
    2         {
    3 
    4             string s = "Hollo World";
    5             Console.WriteLine(s.Substring(6, 2));  //Substring(起始位置,长度) - 从大字符串中,截取小的子串出来。
    6 
    7         }

    显示的结果:

        4、IndexOf("子串") - 从大字符串中,找到子串第一次出现的位置。返回整数。如果大串中找不到小串,返回-1

    1  static void Main(string[] args)
    2         {
    3 
    4             string s = "Hello World";
    5             int b = s.IndexOf("o"); //IndexOf("子串") - 从大字符串中,找到子串第一次出现的位置。返回整数。如果大串中找不到小串,返回-1
    6             Console.WriteLine(b);
    7          }

    显示的结果:

        LastIndexOf("子串") - 从大字符串中,找到子串最后一次出现的位置。返回整数。如果大串中找不到小串,返回-1、

    1  static void Main(string[] args)
    2         {
    3 
    4             string s = "Hello World";//LastIndexOf("子串") - 从大字符串中,找到子串最后一次出现的位置。返回整数。如果大串中找不到小串,返回-1
    5             int b = s.IndexOf("d");
    6             Console.WriteLine(b);
    7             //显示的结果是:10
    8         }

    显示的结果:

        ReplaceWith("被替换的子串","新的子串"):把大串中指定的小串,换成另一小串,返回替换后的大串。

    1   static void Main(string[] args)
    2         {
    3 
    4             string s = "Hello World";
    5             //ReplaceWith("被替换的子串","新的子串"):把大串中指定的小串,换成另一小串,返回替换后的大串。
    6             Console.WriteLine(s.Replace(" ", "_"));   //把空格,替换成下划线。
    7         }

    显示的结果:

    练习1:

    您输入的QQ邮箱是否正确?

     1  static void Main(string[] args)            
     2         {
     3 
     4             Console.WriteLine("请输入您的QQ邮箱:");     //显示打印的
     5 
     6             string youxiang = Console.ReadLine();          //我们要输的
     7          
     8             bool shi = true;
     9 
    10             if (youxiang.EndsWith("@qq.com") == shi)        //判断是不是以@qq.com 结尾的
    11  
    12             {
    13                 Console.WriteLine("您输入的QQ邮箱正确");
    14             }
    15 
    16             else
    17        
    18             {
    19                 Console.WriteLine("你输入的QQ邮箱不正确");
    20             }
    21            
    22        }

    练习2、

    从身份证中找出你的生日

     1 static void Main111111(string[] args) //身份证号,从中找出年、月、日     
     2         {
     3             Console.WriteLine("请您输入身份证号码");
     4             string sfz = Console.ReadLine();
     5 
     6             string nianfen = sfz.Substring(6, 4);      //(6,4)表示第几位的数,取几个。 
     7             string yue = sfz.Substring(10, 2);
     8             string ri = sfz.Substring(12, 2);
     9 
    10             Console.WriteLine("您的生日是:" + nianfen + "" + yue + "" + ri + "");
    11             Console.ReadLine();
    12 
    13         }
  • 相关阅读:
    Python进阶开发之元类编程
    django相关网站
    Ubuntu下vim中文乱码
    django自定义用户表
    C# webbrowser遍历网页元素
    Delphi SetParent 嵌入其他应用程序
    C# DataGirdview手动添加数据,导出txt文件并自动对齐
    C# SetParent将其他程序嵌入自己的程序
    Delphi如何找到出错行的行数!!
    Delphi StringGrid控件的用法
  • 原文地址:https://www.cnblogs.com/yuyu1993/p/5419424.html
Copyright © 2011-2022 走看看