【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语言中 ...
随机推荐
- Viso设置背景
文件 > 形状 > 其它Visio方案 > 背景
- java服务器获取客户端ip
在写服务端代码时,有时需要对客户端ip做认证,比如限制只有某些ip能访问,或者1个ip1天只能访问几次.最近就碰到个需要限制ip的情况,从网上找了一些服务器获取客户端ip的方法,说的都不太完善,这里整 ...
- CentOS 6一键系统优化 Shell 脚本
CentOS 6一键系统优化 Shell 脚本 脚本的内容如下: #!/bin/bash#author suzezhi#this script is only for CentOS 6#check t ...
- HDU5879(打表)
Cure Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- RedisRepository分享和纠错
.mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...
- JS与浏览器的几个兼容性问题
第一个:有的浏览器不支持getElementsByClassName(),所以需要写一个function()来得到需要标签的class,然后进行class的增加.删除等操作. 第二个:在需要得到特定标 ...
- Docker 1.13 管理命令
1.12 CLI 的问题 Docker1.12 命令行接口(CLI)有40多个顶级命令,这些命令存在以下问题: 没有归类组织,这让docker 新手很难学习: 有些命令没有提供足够的上下文环境,以至于 ...
- Wireshark网络抓包(三)——网络协议
一.ARP协议 ARP(Address Resolution Protocol)地址解析协议,将IP地址解析成MAC地址. IP地址在OSI模型第三层,MAC地址在OSI第二层,彼此不直接通信: 在通 ...
- InfluxDB安装及配置
这是我之前整理的InfluxDB安装及配置的笔记,这里记录下,也方便我以后查阅. 环境: CentOS6.5_x64 InfluxDB版本:1.1.0 一.安装 1.二进制安装 这里以centos6. ...
- java_JDBC(3)
Batch和Fetch两个特性非常重要.Batch相当于JDBC的写缓冲,Fetch相当于读缓冲 如果把JDBC类比为JAVA IO的话,不使用Fetch和Batch相当于直接使用FileInputS ...