类函数:string、math
类:系统内置的处理字符串类型的函数方法类。
string是String的快捷方式。所包含的内容都是一样的。
Int i=x.length;//获取一个字符串长度
字符串中,索引号从0开始
String类
字符串当中,索引号是从0开始的
去掉字符串前后空格
Console.Write(a.Trim());
去掉前面的空格
Console.Write(a.TrimStart());
去掉后面的空格
Console.Write(a.TrimEnd());
将所有大写转换为小写
Console.WriteLine(a.ToLower());
将所有小写字母转换为大写
Console.WriteLine(a.ToUpper());
截取字符串,从开始索引截取到最后
Console.WriteLine(a.Substring(5));
截取字符串,从开始索引截取指定的长度
Console.WriteLine(a.Substring(5, 2));
替换
Console.WriteLine(a.Replace("l", "a"));
查找开头第一次出现的索引号,返回值为-1.表示没有找到
Console.WriteLine(a.IndexOf("l"));
从后向前找
Console.WriteLine(a.LastIndexOf("l"));
查看是否以空格开头
Console.WriteLine(a.StartsWith(" "));
查看是否以o结尾
Console.WriteLine(a.EndsWith("o"));
查看是否包含a
Console.WriteLine(a.Contains("a"));
数学类 Math
取上线
double a = 3.14;
Console.WriteLine(Math.Ceiling(a));
取下线
Console.WriteLine(Math.Floor(a));
开平方根
Console.WriteLine(Math.Sqrt(a));
圆周率
Console.WriteLine(Math.PI);
四舍五入
Console.WriteLine(Math.Round(1.5));
Console.WriteLine(Math.Round(2.5));
奇数。5的情况下取上线
偶数。5的情况下取下线
Console.ReadLine();
随机数类 Random
需要使用随机数的时候需要先初始化
Random ran = new Random();
int a = ran.Next(10);
Console.WriteLine(a);
练习:判断邮箱格式是否正确
1.有且只能有一个@2.不能以@开头3.@之后至少有一个.4.@和.不能靠在一起5.不能以.结尾
Console.Write("请输入您的邮箱账号:");
string mail = Console.ReadLine();
if (mail.Contains("@"))
{
int a = mail.IndexOf("@");
int b = mail.LastIndexOf("@");
if (a == b)
{
if (!mail.StartsWith("@"))
{
string mail1 = mail.Substring(a);
if (mail1.Contains("."))
{
wer235fda@163.com
if (mail1.IndexOf(".") != 1&&mail.Substring(a-1,1)!=".")
{
if (!mail.EndsWith("."))
{
Console.WriteLine("输入的邮箱格式正确!您输入的账号是:"+mail);
}
else
{
Console.WriteLine("格式错误!");
}
}
else
{
Console.WriteLine("格式错误!");
}
}
else
{
Console.WriteLine("格式错误!");
}
}
else
{
Console.WriteLine("格式错误!");
}
}
else
{
Console.WriteLine("格式错误!");
}
}
else
{
Console.WriteLine("格式错误!");
}
Console.ReadLine();
验证码:随机出四位验证码
A~Z a~z 0~9
不区分大小写
string ss = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghjklmnopqrstuvwxyz0123456789";
Random ran = new Random();//随机数类的初始化
int a = ran.Next(62);
int b = ran.Next(62);
int c = ran.Next(62);
int d = ran.Next(62);
string aa = ss.Substring(a, 1);
string bb = ss.Substring(b, 1);
string cc = ss.Substring(c, 1);
string dd = ss.Substring(d, 1);
string rect = aa + bb + cc + dd;
string rect = "";
for (int i = 0; i < 4; i++)
{
int a = ran.Next(62);
rect += ss.Substring(a,1);
}
Console.WriteLine("验证码是:" + rect);
Console.Write("请对照输入验证码:");
string shu = Console.ReadLine();
if (shu.ToUpper() == rect.ToUpper())
{
Console.WriteLine("输入正确!");
}
else
{
Console.WriteLine("输入错误!");
}
Console.ReadLine();
输入您的身份证号,打印出您的生日
Console.Write("输入您的身份证号");
string a = Console.ReadLine();
string c = a.Substring(6,4);
string d = a.Substring(10, 2);
string e = a.Substring(12, 2);
Console.WriteLine("您的生日"+c+"年"+d+"月"+e+"日");
Console.ReadLine();
Datetime类 日期时间
若需要使用,首先需要初始化
DateTime dt = new DateTime();
若直接获取当前时间,不用进行初始化
DateTime dt1 = DateTime.Now;
Console.WriteLine(dt);
Console.WriteLine(dt1);
在dt1身上增加10天
Console.WriteLine(dt1.AddDays(10));
增加10个小时
Console.WriteLine(dt1.AddHours(10));
创建时间间隔
TimeSpan time = new TimeSpan(10,10,10,10);
Console.WriteLine(dt1.Add(time));
获取年 dt.Year
获取月 dt.Month
获取日 dt.Day
获取小时 dt.Hour
获取分 dt.Minute
获取秒 dt.Second
Console.WriteLine(dt1.Hour);
DayOfWeek dw = dt1.DayOfWeek;
switch (dw.ToString())
{
case "Monday":
Console.WriteLine("星期一");
break;
}
输入两个时间日期,计算出相差多少天(TotalDays)
Console.Write("请输入你们恋爱的时间:");
DateTime dt = DateTime.Parse(Console.ReadLine());
DateTime dt1 = DateTime.Now;
Console.WriteLine((dt1-dt).TotalDays);
try catch
异常保护语句
Console.Write("请输入一个整数:");
try//尝试
{
int a = int.Parse(Console.ReadLine());
Console.WriteLine(a);
}
catch//若try里面的语句有问题,直接跳到catch执行
{
Console.WriteLine("程序出现错误!");
}
finally//不管对与错,都要执行
{
Console.WriteLine("感谢您的使用!");
}
Console.WriteLine("感谢您的使用!");
类函数:string、math的更多相关文章
- JS中的String.Math.Date
//今天放假没看东西,贴上以前的基础,没事看着玩 // String->-> var myStr = "My name is LiuYashion"; console. ...
- JS中String,Math常用函数
String对象: 1.length属性 说明:获取字符串的长度 实例: var str="abc"; var i=str.length;//output:3 2.charAt() ...
- 【2-26】string/math/datetime类的定义及其应用
一string类 (1)字符串.Length Length作用于求字符串的长度,返回一个int值 (2)字符串.TrimStart(); TrimStart():可删除前空格,返回一个stri ...
- python 加密算法及其相关模块的学习(hashlib,random,string,math)
加密算法介绍 一,HASH Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种 ...
- 原型 Boolean String Math Date知识点
原型 1.定义 每一个对象都有原型 原型仍然是一个对象 模拟实现面向对象的继承性 2.原型链 对象的原型还有原型 对象除了可以使用自有属性还可以继承原型上的属性 3.获取原型 对象.__proto__ ...
- 内置对象(Date String Math Array)
什么是对象 JavaScript 中的所有事物都是对象,如:字符串.数值.数组.函数等,每个对象带有属性和方法. 对象的属性:反映该对象某些特定的性质的,如:字符串的长度.图像的长宽等: 对象的方法: ...
- python加密算法及其相关模块的学习(hashlib,RSA,random,string,math)
加密算法介绍 一,HASH Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种 ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- 60. Permutation Sequence (String; Math)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- js 内置对象参考 (Array,String, Math, Data, Number)
var str = "helloWorld"; var strOne = "helloWorld"; // charAt() 返回在指定位置的字符. var a ...
随机推荐
- [bzoj1430]小猴打架_prufer序列
小猴打架 bzoj-1430 题目大意:题目链接. 注释:略. 想法: 我们发现打架的情况就是一棵树. 我们只需要把确定树的形态然后乘以$(n-1)!$表示生成这棵树时边的顺序. 一共$n$个节点我们 ...
- 洛谷 P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀…
P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀… 题目背景 欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献. 题目描述 You have probably hea ...
- JSP的隐藏对象
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/implicit-objects.html: JSP隐式对象是Java对象,JSP容器使隐式对象在每一个页 ...
- android/java经常使用的工具类源代码
anroid.java经常使用的工具类源代码,当中包含文件操作.MD5算法.文件操作.字符串操作.调试信息log.base64等等. 下载地址:http://download.csdn.net/det ...
- 【c语言】统计一个数字在排序数组中出现的次数
// 题目:统计一个数字在排序数组中出现的次数. // 比如:排序数组{1.2,3,3,3,3,4.5}和数字3,因为3出现了4次.因此输出4 有一种最简单的算法,遍历.可是有比它效率更高的 先看遍 ...
- MySQL-删除数据(DELECT)
数据库备份介绍: 数据库一旦删除数据,它就会永远消失. 因此,在执行DELETE语句之前,应该先备份数据库,以防万一要找回删除过的数据. MySQL提供了非常有用的工具,用于在服务器上本地备份或转储M ...
- 开发,从需求出发 · 之二 造飞机的工厂
CD镇楼~~! 如今.让我们切换到后端开发者的角度看问题.我们须要做的是实现一下这个类,让它返回真实的业务数据. package cn.com.sitefromscrath.service; impo ...
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- [RK3288][Android6.0] 调试笔记 --- Goodix GT9和GT9F区别【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/78341425 Platform: RK3288 OS: Android 6.0 Kernel ...
- ubuntu安装Android Studio
参考 https://developer.android.com/guide/?hl=zh-CN 下载 https://developer.android.com/studio/?hl=zh-CN 解 ...