【2-26】string/math/datetime类的定义及其应用
一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类
例:
- Console.Write("请输入您的邮箱:");
- string id = Console.ReadLine();
- string end = "输入的邮箱正确";
- bool mailonly = true;
- bool mailonlyy = true;
- bool mailstart = true;
- bool mailend = true;
- bool atyd = true;
- bool atsd = true;
- bool dianot = true;
- bool dianno = true;
- bool numno = true;
- int s = id.Length;
- #region 有且只有一个@符号
- int mailonly1 = id.IndexOf("@");
- if (mailonly1 == -)
- {
- mailonlyy = false;
- end = "输入的邮箱有误!";
- }
- else
- {
- string mailonly2 = id.Substring(mailonly1+);
- int mailonly3 = mailonly2.IndexOf("@");
- if (mailonly3 != -)
- {
- mailonly = false;
- end = "输入的邮箱有误!";
- }
- }
- #endregion
- #region 不能以@开头
- bool mailstart1 = id.StartsWith("@");
- if (mailstart1 == true)
- {
- mailstart = false;
- end = "输入的邮箱有误!";
- }
- #endregion
- #region 不能以@结尾
- bool mailend1 = id.EndsWith("@");
- if (mailend1 == true)
- {
- mailend = false;
- end = "输入的邮箱有误!";
- }
- #endregion
- #region@之后必须有点
- if (mailonly1 != -)
- {
- string atyd1 = id.Substring(mailonly1);
- int atyd2 = atyd1.IndexOf(".");
- if (atyd2 == -)
- {
- atyd = false;
- end = "输入的邮箱有误!";
- }
- }
- else
- {
- end = "输入的邮箱有误!";
- }
- #endregion
- #region@之后不能是点
- int atsd1 = id.IndexOf("@.");
- if (atsd1 != -)
- {
- atsd = false;
- end = "输入的邮箱有误!";
- }
- #endregion
- #region 最少一个点,最多两个点
- for (; ; )
- {
- try
- {
- string atyd1 = id.Substring(mailonly1);
- int dianot1 = atyd1.IndexOf(".");
- int a = ;
- if (dianot1 != -)
- {
- a++;
- }
- if (a > )
- {
- dianot = false;
- end = "输入的邮箱有误!";
- break;
- }
- else
- {
- break;
- }
- }
- catch
- {
- end = "输入的邮箱有误!";
- break;
- }
- }
- #endregion
- #region 不能以点结尾
- bool dianno1 = id.EndsWith(".");
- if (dianno1 == true)
- {
- dianno = false;
- end = "输入的邮箱有误!";
- }
- #endregion
- #region 不能以数字结束
- string numno1 = id.Substring(s-);
- try
- {
- int numno2 = Convert.ToInt32(numno1);
- bool b=Convert.ToBoolean(numno2);
- if (b == true)
- {
- numno = false;
- end = "输入的邮箱有误!";
- }
- }
- catch
- {
- }
- #endregion
- if (mailonlyy == false)
- {
- Console.WriteLine("必须有@");
- }
- if (mailonly == false)
- {
- Console.WriteLine("只能有一个@符号");
- }
- if (mailstart == false)
- {
- Console.WriteLine("不能以@开头");
- }
- if (mailend == false)
- {
- Console.WriteLine("不能以@结尾");
- }
- if (atyd == false)
- {
- Console.WriteLine("@之后必须有点");
- }
- if (atsd == false)
- {
- Console.WriteLine("@之后不能是点");
- }
- if (dianot == false)
- {
- Console.WriteLine("最少一个点,最多两个点");
- }
- if (dianno==false)
- {
- Console.WriteLine("不能以点结束");
- }
- if (numno==false)
- {
- Console.WriteLine("不能以数字结束");
- }
- Console.WriteLine(end);
- 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…)累计天、小时等差距
例:
- Console.Write("请输入年:");
- string year = Console.ReadLine();
- Console.Write("请输入月:");
- string month = Console.ReadLine();
- Console.Write("请输入日:");
- string day = Console.ReadLine();
- string s = year + "/" + month + "/" + day;
- try
- {
- DateTime dt = new DateTime();
- dt = DateTime.Parse(s);
- string a = dt.ToString("yyyy年MM月dd日");
- int b = Convert.ToInt32(dt.DayOfWeek);
- int c = dt.DayOfYear;
- string x = "日一二三四五六";
- string x1 = x.Substring(b, );
- Console.WriteLine(a + "是此年的第" + c + "天,星期" + x1);
- DateTime y = new DateTime(, , );
- TimeSpan t = dt - y;
- int t1 = Convert.ToInt32(t.Days);
- int t2 = Math.Abs(t1);
- if (t1 < )
- {
- Console.Write("距离2012年12月24日世界末日还有" + t2 + "天");
- }
- else if (t1 > )
- {
- Console.Write("世界末日过去了" + t1 + "天");
- }
- else
- {
- Console.Write("您输入的是世界末日!!!");
- }
- }
- catch
- {
- Console.WriteLine("输入的时间日期有误!");
- }
- Console.ReadLine();
【2-26】string/math/datetime类的定义及其应用的更多相关文章
- 类之string类、Math类、DateTime类
String类 string a = "abcdef123456"; 注:字符串的长度是从0开始计数的如:0,1,2,3,4,5,6,7,8,9........ a.Length; ...
- 【2017-02-26】String类、Math类、DateTime类
一.String类 黑色小扳手 - 属性 后面不带括号紫色立方体 - 方法 后面带括号 字符串.Length - 字符串长度,返回int类型 字符串.TrimStart() - 去 ...
- 【2017-2-26】C#String类、Math类、DateTime类
String类 黑色小扳手:属性 后面不带括号 紫色小箱子:方法 后面带小括号 1.字符串.Length; 字符串长度,返回int类型 字符串的长度 2.字符串.TrimSta ...
- C#string类;math类;datetime类
String类: .Length字符的长度 .Trim()去掉开头以及结尾的空格 .TrimStart()去掉字符串开头的空格 .TrimEnd()去掉字符串后面的空格 .ToUpper()全 ...
- string、math、random、datetime类
1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确 ...
- (C++)已知String类的定义,实现其函数体
CString类的定义如下: class CMyString{ public: CMyString(const char* pData=NULL); CMyString(const CMyString ...
- java实验作业类的定义与描述
1 //1三角形的定义与描述 2 package test; 3 4 public class sjx { 5 private double a,b,c; 6 7 public sjx(double ...
- string转DateTime(时间格式转换)
1.不知道为什么时间在数据库用varchar(8)来保存,例如"19900505",但是这样的保存格式在处理时间的时候是非常不方便的. 但是转换不能用Convert.ToDateT ...
- JavaScript 类的定义和引用 JavaScript高级培训 自定义对象
在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等. 一,概述 在Java语言中 ...
随机推荐
- ORACLE获取字符串中数字部分
') from dual; select regexp_replace('23456中国3-00=.,45','[^0-9]') from dual;标签:regexp_replace regexp ...
- 关于redis的主从、哨兵、集群
关于redis主从.哨兵.集群的介绍网上很多,这里就不赘述了. 一.主从 通过持久化功能,Redis保证了即使在服务器重启的情况下也不会损失(或少量损失)数据,因为持久化会把内存中数据保存到硬盘上,重 ...
- opp(Object Oriented Programming)
嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙. 好了,废话也不多说,把自己学到的放上来吧.嗯,说什么好呢,就说原型链啊 原型对象 每个 ...
- 语句 if else
语句 语句是指程序命令,都是按照顺序执行的.语句在程序中的执行顺序称为“控制流”或“执行流”. 根据程序对运行时所收到的输入的响应,在程序每次运行时控制流可能有所不同. 语句间的标点符号必须是英文标点 ...
- 第二章:在HTML中使用JavaScript
1:在使用<script>嵌入JavaScript代码死,记住不要在代码中的任何地方出现"</script>"字符串 例如,浏览器在加载下面所示代码时就会产 ...
- ArcGIS快速制图插件介绍
ArcGIS快速制图插件介绍 By 李远祥 作品背景 <快速制图插件增强版>在原有的<快速制图插件>基础上,加入植被乱序填充.生成立体楼快.等高线增强显示.一键导出地图和数据. ...
- redisson实现分布式锁原理
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- pch 文件
PCH的文件的用途: 在实际的项目开发中,如果很多地方都在使用某个类的头文件,很多地方都在使用同一个”宏”的时候:很多地方用到了NSLog()函数, 在app发布的时候,想清除掉时,此时就需 ...
- C/C++中慎用宏(#define)
宏的定义在程序中是非常有用的,但是使用不当,就会给自身造成很大的困扰.通常这种困扰为:宏使用在计算方面. 本例子主要是在宏的计算方面,很多时候,大家都知道定义一个计算的宏,对于编译和编程是多么的有用. ...
- C++模板杂谈
在模板编程中,有几个常用的技术:模板(偏)特化,特性萃取,标签分派,匹配失败不是错误.其中模板(偏)特化是基础,匹配失败不是错误(SFINAE)应用最为广泛. 现代C++对模板编程做了更多的加强,bo ...