zoukankan      html  css  js  c++  java
  • 类 string math

    //String Math
    string a = " abCdefgd8 ";
    int c = a.IndexOf("d");//从前面开始找,找到第一个,数它的索引号
    int d = a.LastIndexOf("d");
    bool b = a.Contains("dd");//是否包含此字符串
    Console.WriteLine(d);

    int b = a.Length;//长度
    Console.WriteLine(b);
    string c = a.Trim();//去掉前后空格
    Console.Write(c);
    Console.Write(" ");
    string d = a.TrimStart();//去掉前空格
    Console.Write(d);
    Console.Write(" ");
    string e = a.TrimEnd();//去掉后空格
    Console.Write(e);
    Console.Write(" ");
    string f = a.ToUpper();//全部将小写字母转换为大写
    Console.WriteLine(f);
    string g = a.ToLower();//全部将大写字母转换为小写
    Console.WriteLine(g);

    索引号是从0开始的

    string h = a.Substring(4);
    //里面一个值,表示从这个索引开始一直截取到最后
    Console.WriteLine(h);
    Console.WriteLine(a.Substring(8));

    //a = a.Substring(8);//若果不重新赋值,a是没有变化的

    //两个值,表示从哪个索引号开始,截取多少长度
    string i = a.Substring(4, 3);
    Console.WriteLine(i);


    a = a.Replace("de", "DE");

    Console.WriteLine(a);


    string j = "2012 12 23";
    string[] aa = j.Split();//分割字符串,以什么字符
    foreach (string m in aa)
    {
    Console.WriteLine(m);
    }

    Math类
    double a = 1.5;
    Console.WriteLine(Math.Ceiling(a));//取上线
    Console.WriteLine(Math.Floor(a));//取下线
    Console.WriteLine(Math.PI*a);//π 圆周率
    Console.WriteLine(Math.Sqrt(a));//开平方根
    Console.WriteLine(Math.Round(a));//四舍五入
    //注意:奇数.5的情况下,取上线
    //偶数.5的情况下,取下线

    案例

    //输入身份证号,截取生日,输出

    for (; ; )
    {
    Console.Write("请输入身份证号码:");
    string a = Console.ReadLine();
    if (a.Length == 18)
    {
    int b = int.Parse(a.Substring(6, 4));
    int c = int.Parse(a.Substring(10, 2));
    int d = int.Parse(a.Substring(12, 2));
    Console.WriteLine(b + "年" + c + "月" + d + "日");
    break;
    }
    else
    {
    Console.WriteLine("您的输入有误!");
    }
    }
    Console.ReadLine();

    案例

    // 练习:判断邮箱格式是否正确
    //1.有且只能有一个@
    //2.不能以@开头
    //3.@之后至少有一个.
    //4.@和.不能靠在一起
    //5.不能以.结尾
    Console.Write("请输入你的邮箱:");
    string a = Console.ReadLine();
    if (a.Contains("@"))
    {
    int c = a.IndexOf("@");
    int d = a.LastIndexOf("@");
    if (d == c)
    {
    string e = a.Substring(c);

    if (e.Contains("."))
    {
    if (e.IndexOf(".") != 1)
    {
    if (e.LastIndexOf(".") != e.Length - 1)
    {
    Console.WriteLine("您输入的邮箱格式正确!");
    }
    else
    {
    Console.WriteLine("您的输入有误");
    }
    }
    else
    {
    Console.WriteLine("您的输入有误");
    }

    }
    else
    {
    Console.WriteLine("您的输入有误");
    }
    }
    else
    {
    Console.WriteLine("您的输入有误");
    }
    }
    else
    { Console.WriteLine("您的输入有误"); }

    Console.ReadLine();

    //随机数类 Random

    Random ran = new Random();
    int a=ran.Next(10);
    Console.WriteLine(a);

    案例//随机出验证码,对照输入,判断是否正确,错误重新输入
    string s = "abcdefghijklmnopqrstubwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for (; ; )
    {
    Random ran = new Random();
    string y = "";
    for (int i = 1; i <= 4; i++)
    {
    y += s.Substring(ran.Next(s.Length), 1);
    }
    Console.WriteLine(y);
    Console.Write("请输入验证码:");
    string y1 = Console.ReadLine();
    if (y.ToLower() == y1.ToLower())
    {
    Console.WriteLine("您的输入正确");
    break;
    }
    else
    {
    Console.Clear();
    Console.WriteLine("您的输入有误,请重新输入:");
    }
    }

    Console.ReadLine();

     

  • 相关阅读:
    Network(树形dp)洛谷2899
    2590 树的统计
    LCT 最小生成树
    几种贪心小结
    snmp
    div页面跳转
    2017.11.2总结,回顾及成果
    2017.11.1知识总结及回顾
    check,form,单选框与复选框总结
    HTML空格字符
  • 原文地址:https://www.cnblogs.com/nannan-0305/p/5268457.html
Copyright © 2011-2022 走看看