zoukankan      html  css  js  c++  java
  • 7、C#基础整理(类)

    String类

    概念:是一个class类型的类,里面包含许多处理字符串的方法和属性

    1、length方法。 例:

    string s = "hello";
    Console.WriteLine("s的长度为{0}",s.Length);//获取字符串长度,返回int值

    2、Trim & ToUpper方法。例:

    string i = " hello ";
    Console.WriteLine("i的值为:{0}",i+"a");
    Console.WriteLine("i去除空格后的值为:{0}",i.Trim()+"a");
    Console.WriteLine("i去除左边空格后的值为:{0}",i.TrimStart()+"a");
    Console.WriteLine("i去除右边空格后的值为:{0}",i.TrimEnd()+"a");
    Console.WriteLine("i大写形式为:{0}", i.ToUpper());

    3、indexof :从0开始的索引。例:

    string ss = "abcdefc";
    Console.WriteLine(ss.IndexOf("c"));//ctrl+shift+空格 第一个匹配项的首字母索引
    Console.WriteLine(ss.LastIndexOf("c"));//最后一个匹配项的首字母索引

    4、startswith  endswith:判断是否以某个字符串开头/结尾,返回布尔值。例:

    bool b1 = ss.StartsWith("ab");
    bool b2 = ss.EndsWith("fc");
    Console.WriteLine(b1+","+b2);

    5、contains:判断字符串中是否包含某个字符段,返回布尔值。例:

    Console.WriteLine(ss.Contains("bc"));

    6、substring:截取字符串。例:

    Console.WriteLine("从第2个索引开始的3个字符为{0}",ss.Substring(2, 3));//从指定索引“2”开始打印长度“3”个字符
    Console.WriteLine("从第二个索引开始一直到最后的字符串为{0}", ss.Substring(2));//从指定索引“2”截取到最后

    7、tostring:转换成字符串。例:

    DateTime date = DateTime.Now;
    string sss = date.ToString("yyyy年MM月dd日hh时mm分ss秒");
    Console.WriteLine(sss);
    double dd = 1.234;
    string sss1 = dd.ToString("#.00");//小数点后有几个#取几位数,小数点前的#取所有位数,如果小数点后面是0,用“.00”的时候补零(如果不是0会显示原数),“.##”不会补零
    Console.WriteLine(sss1);

    Math类

    是指数学运算的各种方法,大家可以尝试输入Math.查看它的方法,此处我只写一个:

    Math.Floor/Celing:地板值(最小值)、天花板值(最大值)。例:

    Console.WriteLine("3.14的地板值为{0}",Math.Floor(3.14));//3.00
    Console.WriteLine("3.14的天花板值为{0}",Math.Ceiling(3.14));//4.00

    Datetime类

    1、now: 获取系统当前时间

    DateTime dt = DateTime.Now;
    dt = dt.AddYears(3);//在当前时间下加三年(同时也可以写AddMonths,AddDays,AddHours,等)
    Console.WriteLine(dt);//输出结果比当前时间多加了三年

    2、与TimeSpan的合用:

    DateTime da = new DateTime(1990, 01, 01);
    TimeSpan t = new TimeSpan(2,10,20,0) ;//TimeSpan(days,hours,minutes,seconds)
    da = da.Add(t);
    Console.WriteLine(da);

    Console.Clear();//清空控制台上的所有信息

    练习

    1、输入一个身份证号,截取生日

    Console.WriteLine("请输入身份证号:");
    string id = Console.ReadLine();
    if (id.Length == 18)
    {
        Console.WriteLine("生日为:{0}年{1}月{2}日",id.Substring(6,4),id.Substring(10,2),id.Substring(12,2));
    }
    else
        Console.WriteLine("您的输入有误");
    查看答案

    2、随机生成四位验证码(0~9,a~Z)

    Random r = new Random();
    string yan = "0123456789abcdefghjklmnopqistuvwxyzABCDEFGHIJKLMNOPQISTUVWXYZ";
    string yzm = "";
    for (int j = 0; j < 4; j++)
    {
        int ra = r.Next(yan.Length);
        yzm = yan.Substring(ra, 1)+yzm;
    }
    Console.WriteLine(yzm);
    查看答案

    3、通过刷屏的简单手机号摇奖方法

                在最顶上写上using Threading

                for (int j = 0; j < 20; j++)
                {
                    Thread.Sleep(100);
                    Console.Clear();
                    string shouji = "133333333333333";
                    Console.WriteLine(shouji);
                    Thread.Sleep(100);
                    Console.Clear();
                    shouji = "13344444444444444";
                    Console.WriteLine(shouji);
                }    
  • 相关阅读:
    hihoCoder #1062 : 最近公共祖先·一
    hihoCoder #1050 : 树中的最长路
    hihoCoder #1049 : 后序遍历
    108 Convert Sorted Array to Binary Search Tree 将有序数组转换为二叉搜索树
    107 Binary Tree Level Order Traversal II 二叉树的层次遍历 II
    106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树
    105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
    104 Maximum Depth of Binary Tree 二叉树的最大深度
    102 Binary Tree Level Order Traversal 二叉树的层次遍历
    101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
  • 原文地址:https://www.cnblogs.com/wleaves/p/4170095.html
Copyright © 2011-2022 走看看