string

(1)字符串.Length    Length作用于求字符串的长度,返回一个int值

(2)字符串.TrimStart();  TrimStart();可删除前空格,返回一个string类

(3)字符串.TrimEnd();   TrimEnd(); 可删除后空格,也返回一个string类

(4) ***字符串.Trim();      Trim(); 删除前后空格,返回string类

字母大小写:

(5)****字符串.ToUpper();  ToUpper();  将字符串大写,返回string类

(6)****字符串.ToLower();  ToLower( ); 将字符串小写,返回string类

截取:

(7)***字符串.SubString(a);  将字符串从下标a开始截取后面部分

(8)****字符串.SubString(a,b);  将字符串从下标a开始截取后面b个字符,返回string类

注意:下标索引从0开始

截取的对象下标、长度必须在字符串范围之内,否则报错。

索引:

(9)***字符串.IndexOf(“字符串a”);  从字符串中索引第一个字符串a,返回int值

(10)****字符串.IndexOf(“字符串a”,int b );  从b值开始索引字符串a, 返回int值

(11)***字符串.LastIndexOf(“字符串a”);  从最后开始索引,索引出最后一个字符串a,    返回int值

注意:如果字符串中不存在索引对象,则返回-1;

比较判断字符:

(12)***字符串.StartWidth("字符串a");  判断字符串是否以“字符串a”开头

(13)***字符串.EndsWidth("字符串b"); - 判断字符串是否以“字符串b”结尾

(14)**字符串.Contains("字符串c"); - 判断字符串是否包含“字符串c”,返回bool类

移除、替换:

(15)****变量.Replace(要替换的字符串a,替换成的字符串b);  字符替换  返回string类

(16)s.Remove(索引int值); 移除从索引到末尾的全部字符  返回string类

例:

  1. Console.Write("请输入您的邮箱:");
  2. string id = Console.ReadLine();
  3. string end = "输入的邮箱正确";
  4. bool mailonly = true;
  5. bool mailonlyy = true;
  6. bool mailstart = true;
  7. bool mailend = true;
  8. bool atyd = true;
  9. bool atsd = true;
  10. bool dianot = true;
  11. bool dianno = true;
  12. bool numno = true;
  13. int s = id.Length;
  14.  
  15. #region 有且只有一个@符号
  16. int mailonly1 = id.IndexOf("@");
  17. if (mailonly1 == -)
  18. {
  19. mailonlyy = false;
  20. end = "输入的邮箱有误!";
  21. }
  22. else
  23. {
  24. string mailonly2 = id.Substring(mailonly1+);
  25. int mailonly3 = mailonly2.IndexOf("@");
  26. if (mailonly3 != -)
  27. {
  28. mailonly = false;
  29. end = "输入的邮箱有误!";
  30. }
  31. }
  32. #endregion
  33.  
  34. #region 不能以@开头
  35.  
  36. bool mailstart1 = id.StartsWith("@");
  37. if (mailstart1 == true)
  38. {
  39. mailstart = false;
  40. end = "输入的邮箱有误!";
  41. }
  42. #endregion
  43.  
  44. #region 不能以@结尾
  45. bool mailend1 = id.EndsWith("@");
  46. if (mailend1 == true)
  47. {
  48. mailend = false;
  49. end = "输入的邮箱有误!";
  50. }
  51. #endregion
  52.  
  53. #region@之后必须有点
  54. if (mailonly1 != -)
  55. {
  56. string atyd1 = id.Substring(mailonly1);
  57. int atyd2 = atyd1.IndexOf(".");
  58. if (atyd2 == -)
  59. {
  60. atyd = false;
  61. end = "输入的邮箱有误!";
  62. }
  63. }
  64. else
  65. {
  66. end = "输入的邮箱有误!";
  67. }
  68. #endregion
  69.  
  70. #region@之后不能是点
  71. int atsd1 = id.IndexOf("@.");
  72. if (atsd1 != -)
  73. {
  74. atsd = false;
  75. end = "输入的邮箱有误!";
  76. }
  77. #endregion
  78.  
  79. #region 最少一个点,最多两个点
  80. for (; ; )
  81. {
  82. try
  83. {
  84. string atyd1 = id.Substring(mailonly1);
  85. int dianot1 = atyd1.IndexOf(".");
  86. int a = ;
  87. if (dianot1 != -)
  88. {
  89. a++;
  90. }
  91. if (a > )
  92. {
  93. dianot = false;
  94. end = "输入的邮箱有误!";
  95. break;
  96. }
  97. else
  98. {
  99. break;
  100. }
  101. }
  102. catch
  103. {
  104. end = "输入的邮箱有误!";
  105. break;
  106. }
  107. }
  108. #endregion
  109.  
  110. #region 不能以点结尾
  111.  
  112. bool dianno1 = id.EndsWith(".");
  113. if (dianno1 == true)
  114. {
  115. dianno = false;
  116. end = "输入的邮箱有误!";
  117. }
  118.  
  119. #endregion
  120.  
  121. #region 不能以数字结束
  122.  
  123. string numno1 = id.Substring(s-);
  124. try
  125. {
  126. int numno2 = Convert.ToInt32(numno1);
  127. bool b=Convert.ToBoolean(numno2);
  128. if (b == true)
  129. {
  130. numno = false;
  131. end = "输入的邮箱有误!";
  132. }
  133. }
  134. catch
  135. {
  136. }
  137. #endregion
  138. if (mailonlyy == false)
  139. {
  140. Console.WriteLine("必须有@");
  141. }
  142. if (mailonly == false)
  143. {
  144. Console.WriteLine("只能有一个@符号");
  145. }
  146. if (mailstart == false)
  147. {
  148. Console.WriteLine("不能以@开头");
  149. }
  150. if (mailend == false)
  151. {
  152. Console.WriteLine("不能以@结尾");
  153. }
  154. if (atyd == false)
  155. {
  156. Console.WriteLine("@之后必须有点");
  157. }
  158. if (atsd == false)
  159. {
  160. Console.WriteLine("@之后不能是点");
  161. }
  162. if (dianot == false)
  163. {
  164. Console.WriteLine("最少一个点,最多两个点");
  165. }
  166. if (dianno==false)
  167. {
  168. Console.WriteLine("不能以点结束");
  169. }
  170. if (numno==false)
  171. {
  172. Console.WriteLine("不能以数字结束");
  173. }
  174. Console.WriteLine(end);
  175. Console.ReadLine();

math

(1)Math.Pow(变量值x,幂y); 求变量值x的y幂次方;x,y为double类型,返回double类

(2)Math.Sqrt(变量值x);  求变量值x的平方根,返回double类

(3)Math.Ceiling(变量值x);   取变量值X(double类)上限

(4)Math.Floor(变量值x);    取变量值x(double类)下限

(5)Math.Round(变量值x);  四舍五入,当整数部分为奇数5入;当整数部分为奇数5舍

(6)Math.Abs(变量值x);     取变量值x的绝对值

三DateTime

(1)DateTime 变量名 = new DateTime(); - 定义变量类型

变量名 = DateTime.Now; - 获取此电脑当前时间

(2)变量.ToString("格式"); - 设置日期格式化

格式:yyyy-年 MM-月 dd-日 hh-12制小时 HH-24制小时 mm-分钟 ss-秒  ms-毫秒

(3)时间加

1、变量.AddYears(int值); - 在此时间基础上增加int值年

2、变量.AddMonths(int值); - 在此时间基础上增加int值月

3、变量.AddDays(int值); - 在此时间基础上增加int值日

4、变量.AddHours(); - 在此时间基础上增加int值小时

5、变量.AddMinutes(); - 在此时间基础上增加int值分钟

6、变量.AddSeconds(); - 在此时间基础上增加int值秒

(4)截取部分时间变量

1、时间变量.Year; - 获取此时间变量的年份

2、时间变量.Month; - 获取月份

3、时间变量.Day; - 日

4、时间变量.Hour; - 小时

5、时间变量.Minute; - 分钟

6、时间变量.Second; - 秒

7、时间变量.Millisecond; - 毫秒

5)获取变量天数、星期

1、时间变量.DayOfYear; - 获取当前日期是此年中的第几天

2、时间变量.DayOfWeek; - 获取是星期几

3、时间变量.TimeOfDay; - 获取时间部分

4、时间变量.Date; - 获取日期部分

(6)TimeSpan类型 - 时间间隔类型

1、时间变量.Days - 差距多少天

2、时间变量.Hours - 一天中差距多少小时

3、时间变量.Minutes - 一天中差距多少分钟

4、时间变量.Seconds - 一天中差距多少秒

5、时间变量.Milliseconds - 毫秒

6、时间变量.Total(Days、Hours、Minutes…)累计天、小时等差距

例:

  1. Console.Write("请输入年:");
  2. string year = Console.ReadLine();
  3. Console.Write("请输入月:");
  4. string month = Console.ReadLine();
  5. Console.Write("请输入日:");
  6. string day = Console.ReadLine();
  7. string s = year + "/" + month + "/" + day;
  8. try
  9. {
  10. DateTime dt = new DateTime();
  11. dt = DateTime.Parse(s);
  12. string a = dt.ToString("yyyy年MM月dd日");
  13. int b = Convert.ToInt32(dt.DayOfWeek);
  14. int c = dt.DayOfYear;
  15. string x = "日一二三四五六";
  16. string x1 = x.Substring(b, );
  17. Console.WriteLine(a + "是此年的第" + c + "天,星期" + x1);
  18. DateTime y = new DateTime(, , );
  19. TimeSpan t = dt - y;
  20. int t1 = Convert.ToInt32(t.Days);
  21. int t2 = Math.Abs(t1);
  22. if (t1 < )
  23. {
  24. Console.Write("距离2012年12月24日世界末日还有" + t2 + "天");
  25. }
  26. else if (t1 > )
  27. {
  28. Console.Write("世界末日过去了" + t1 + "天");
  29. }
  30. else
  31. {
  32. Console.Write("您输入的是世界末日!!!");
  33. }
  34.  
  35. }
  36. catch
  37. {
  38. Console.WriteLine("输入的时间日期有误!");
  39. }
  40.  
  41. Console.ReadLine();

【2-26】string/math/datetime类的定义及其应用的更多相关文章

  1. 类之string类、Math类、DateTime类

    String类 string a = "abcdef123456"; 注:字符串的长度是从0开始计数的如:0,1,2,3,4,5,6,7,8,9........ a.Length; ...

  2. 【2017-02-26】String类、Math类、DateTime类

    一.String类 黑色小扳手 - 属性     后面不带括号紫色立方体 - 方法     后面带括号 字符串.Length  -  字符串长度,返回int类型 字符串.TrimStart() - 去 ...

  3. 【2017-2-26】C#String类、Math类、DateTime类

    String类 黑色小扳手:属性      后面不带括号 紫色小箱子:方法      后面带小括号 1.字符串.Length;   字符串长度,返回int类型 字符串的长度 2.字符串.TrimSta ...

  4. C#string类;math类;datetime类

    String类: .Length字符的长度   .Trim()去掉开头以及结尾的空格 .TrimStart()去掉字符串开头的空格 .TrimEnd()去掉字符串后面的空格   .ToUpper()全 ...

  5. string、math、random、datetime类

    1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确            ...

  6. (C++)已知String类的定义,实现其函数体

    CString类的定义如下: class CMyString{ public: CMyString(const char* pData=NULL); CMyString(const CMyString ...

  7. java实验作业类的定义与描述

    1 //1三角形的定义与描述 2 package test; 3 4 public class sjx { 5 private double a,b,c; 6 7 public sjx(double ...

  8. string转DateTime(时间格式转换)

    1.不知道为什么时间在数据库用varchar(8)来保存,例如"19900505",但是这样的保存格式在处理时间的时候是非常不方便的. 但是转换不能用Convert.ToDateT ...

  9. JavaScript 类的定义和引用 JavaScript高级培训 自定义对象

    在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等.     一,概述 在Java语言中 ...

随机推荐

  1. ORACLE获取字符串中数字部分

    ') from dual; select regexp_replace('23456中国3-00=.,45','[^0-9]') from dual;标签:regexp_replace regexp ...

  2. 关于redis的主从、哨兵、集群

    关于redis主从.哨兵.集群的介绍网上很多,这里就不赘述了. 一.主从 通过持久化功能,Redis保证了即使在服务器重启的情况下也不会损失(或少量损失)数据,因为持久化会把内存中数据保存到硬盘上,重 ...

  3. opp(Object Oriented Programming)

    嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙. 好了,废话也不多说,把自己学到的放上来吧.嗯,说什么好呢,就说原型链啊 原型对象 每个 ...

  4. 语句 if else

    语句 语句是指程序命令,都是按照顺序执行的.语句在程序中的执行顺序称为“控制流”或“执行流”. 根据程序对运行时所收到的输入的响应,在程序每次运行时控制流可能有所不同. 语句间的标点符号必须是英文标点 ...

  5. 第二章:在HTML中使用JavaScript

    1:在使用<script>嵌入JavaScript代码死,记住不要在代码中的任何地方出现"</script>"字符串 例如,浏览器在加载下面所示代码时就会产 ...

  6. ArcGIS快速制图插件介绍

    ArcGIS快速制图插件介绍 By 李远祥 作品背景 <快速制图插件增强版>在原有的<快速制图插件>基础上,加入植被乱序填充.生成立体楼快.等高线增强显示.一键导出地图和数据. ...

  7. redisson实现分布式锁原理

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. pch 文件

    PCH的文件的用途:      在实际的项目开发中,如果很多地方都在使用某个类的头文件,很多地方都在使用同一个”宏”的时候:很多地方用到了NSLog()函数, 在app发布的时候,想清除掉时,此时就需 ...

  9. C/C++中慎用宏(#define)

    宏的定义在程序中是非常有用的,但是使用不当,就会给自身造成很大的困扰.通常这种困扰为:宏使用在计算方面. 本例子主要是在宏的计算方面,很多时候,大家都知道定义一个计算的宏,对于编译和编程是多么的有用. ...

  10. C++模板杂谈

    在模板编程中,有几个常用的技术:模板(偏)特化,特性萃取,标签分派,匹配失败不是错误.其中模板(偏)特化是基础,匹配失败不是错误(SFINAE)应用最为广泛. 现代C++对模板编程做了更多的加强,bo ...