• 类中的两大类(string类、math类)的应用


    类是我们在学习C#的过程中很关键也是特别容易让人蒙逼得地方,类的应用直接可以调用它的属性和方法来进行判断和验证

    string类(也叫字符串类)

    C#中的String类很有用,下面是一些它的常用方法的总结,如果灵活运用这些的话,String类就掌握的差不多了。

    .ToUpper()    //转为大写"AbC" -->"ABC"

    1             string shu =Console .ReadLine  ();
    2             string shu1 = shu.ToUpper();
    3             Console.WriteLine(shu1);

    将shu中的小写改为大写

    .ToLower()    //转为小写字符串"AbC"-->"abc"
    1             string shu =Console .ReadLine  ();
    2             string shu1 = shu.ToLower();
    3             Console.WriteLine(shu1);

    将shu中的大写改为小写

    .Trim()       //去掉字符串首尾的空格"  abc "-->"abc"

    1             string shu = "  aaaaa";
    2             string shu1 = shu.Trim ();
    3             Console.WriteLine(shu1);
    4                     Console.ReadLine();
    5         }

    去掉aaaaa前面的空格

    .Replace(a,b)  //替换字符串中的字符,如:'a'替换为'b'

    1 string shu = "  aaaaa";
    2             string shu1 = shu.Replace("a","b");
    3             Console.WriteLine(shu1);
    4                     Console.ReadLine();

    .SubString(int startIndex)            //从指定序号开始,一直到最后,组成的字符串

    例子是从第二个a开始截取后面的字符串组合

    索引就是每个字母的下表,每个字符的下标都是该字符从做往右数位置所在减一

    1  string shu = "aaaaa";
    2             string shu1 = shu.Substring (1);
    3             Console.WriteLine(shu1);
    4                     Console.ReadLine();


    .SubString(int startIndex,int length) //从指定序号startIndex,连续取length个,如果超过长度会报异常

    下面的意思是从索引为1(也就是第二个a开始截取,截取包括a在内的两个字符)

    1  string shu = "aaaaa";
    2             string shu1 = shu.Substring (1,2);
    3             Console.WriteLine(shu1);
    4                     Console.ReadLine();

    .Contains(char c)      // 是否包含 字符
    .Contains(string str)  // 是否包含 子字符串

    查找shu这个字符串中有没有b,当有b时显示true,相反则没有

    1   string shu = "aaaab";
    2             bool shu1 = shu.Contains("b");
    3             Console.WriteLine(shu1);
    4                     Console.ReadLine();

    .StartsWith(string str) //是否以str开头
    .EndsWith(string str)   //是否以str结尾

    是不是一字符“a”开头

    1  string shu = "aaaab";
    2             bool shu1 = shu.StartsWith ("a");
    3             Console.WriteLine(shu1);
    4                     Console.ReadLine();

    .IndexOf(char c)        //找到第一个字符c的index,如果没找到返回-1
    .IndexOf(string str)    //找到第一个字符串str的位置

    在字符串shu中找c的位置,如果没有就返回int值为-1

    1             string shu = "aaaab";
    2            int shu1 = shu.IndexOf ("c");
    3             Console.WriteLine(shu1);
    4                     Console.ReadLine();

    math类

    Math.Pow(x,y);

    求一个数的次方

    Math.Sqrt(x);

    求一个数的平方根

    Math.Ceiling(double);

    当为整数取数时,小数点后>0时取小数加1
    Math.Floor(double);

    当为整数取数时,不管小数是多少去掉小数取整数
    Math.Round(double);

    四舍五入:注意当整数部分为奇数.5加1,整数部分为偶数.5舍去
    Math.Abs(double);

    求绝对值

    Date Time类

    Date Time dt=new  Date Time(1999,12,11);

    获得当前系统时间: DateTime dt = DateTime.Now;
    Environment.TickCount可以得到“系统启动到现在”的毫秒值
    DateTime now = DateTime.Now;

    .Tostring();初始化要求按照那种方式输出
    Console.WriteLine(now.ToString("yyyy-MM-dd"));  //按yyyy-MM-dd格式输出

    计算某年某月的天数
    int days = DateTime.DaysInMonth(2009, 8);       
    days = 31;                                      
    给日期增加一天、减少一天
    DateTime dt =DateTime.Now;
    dt.AddDays(1); //增加一天 dt本身并不改变
    dt.AddDays(-1);//减少一天 dt本身并不改变

    dt.Addhours(1.5);增加1.5小时 dt本身并不改变

    dt.AddMonths(1);增加1月 dt本身并不改变

    dt.AddYears(1);增加1年dt本身并不改变

    AddMinutes(); - 增加分钟
    .AddSeconds(); - 增加秒

    .Year; - 获取此时间变量的年份
    .Month; - 获取月份
    .Day; - 日
    .Hour; - 小时
    .Minute; - 分钟
    .Second; - 秒
    .Millisecond; - 毫秒

    .DayOfYear; - 获取当前日期是此年中的第几天
    .DayOfWeek; - 获取是星期几

    .TimeOfDay; - 获取时间部分
    .Date; - 获取日期部分


    TimeSpan类型 - 时间间隔类型
    .Days - 差距多少天
    .Hours - 一天中差距多少小时
    .Minutes - 一天中差距多少分钟
    .Seconds - 一天中差距多少秒
    .Milliseconds - 毫秒

    .Total.... 累计差距

  • 相关阅读:
    VC各种链接错的解决办法【转】http://www.2cto.com/kf/201203/124100.html
    error LNK2019: 无法解析的外部符号 _XXX,该符号在函数 XXX 中被引用
    CVTRES : fatal error CVT1100: 资源重复。类型: BITMAP LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    C++控制台没有引用的头文件也会编译的原因
    关于VS2008编译错误"error LNK2005: 已经在 .obj 中定义" 【转】http://akheyun.blog.163.com/blog/static/138249276201062221452697/
    IIS下PHP的ISAPI和FastCGI比较
    VC++常规错误1>nafxcwd.lib(afxmem.obj) 【转】
    不能将参数 2 从“const char *”转换为“LPCWSTR”【转】http://blog.sina.com.cn/s/blog_4a94a0db0100ktxp.html
    深入探究VC —— 编译器cl.exe(1)【转】http://blog.csdn.net/wangningyu/article/details/4830920
    Asp.net MVC Routing Debugger的使用
  • 原文地址:https://www.cnblogs.com/zhulijun/p/6476100.html
Copyright © 2020-2023  润新知