一、正则表达式
- 正则表达式中有三种类型的括号:方括号“[ ]”内是需要匹配的字符,花括号“{ }”内是指定匹配的字符数量
- 插入符号“^”表示正则式的开始
- 美元符号“$”表示正则式的结束
Regex obj=new Regex("[a-z]{10}")//搜索长度为10 的a-z的英文字母
1. 创建正则
字符串可以使用 @" " 进行创建 这种字符串的内容将会忽略转义字符并保留格式 写什么就是什么。正则表达式就是一个字符串 只是它是用字符串表示的一种匹配模式 \d匹配数字 \w匹配单词的模式…
因为正则需要使用 \x 的模式 因此不能使用普通的字符串创建,程序会认识为\x式一种转义 所以一般使用 @" "
string s1 = "aaa\nbbb"; // \n 为换行
Console.WriteLine(s1);
string s2 = @"aaa\nbbb"; // \n 为子字符串
Console.WriteLine(s2);
2.可以使用Replace方法进行替换 正则匹配到的字符
string string1 = "123abc123abccba";
Regex reg1 = new Regex(@"a"); //匹配字符a,替换为*
Console.WriteLine(reg1.Replace(string1,"*"));
3.正则查询 Match Matches
/ \u002f 等同于 /
string str = "123的骄?傲asjdk\u002f";
Console.WriteLine(str);
//正则
Regex reg = new Regex(@"[\d/]");
Match m = reg.Match(str);
Console.WriteLine(m.Value);
Console.WriteLine(reg.Match(str));
Matches 用于从指定的字符串中查询满足条件的所有匹配项
//MatchCollection 类表示成功的非重叠匹配的只读的集合
MatchCollection ms = reg.Matches(str);
foreach (Match item in ms)
{
Console.WriteLine(item.Value);//遍历输出所有匹配项
}
// 获取匹配项的总数量
Console.WriteLine(ms.Count);
二、堆栈
- 值类型的储存只需要一段储存 用来存放实际的数据
- 引用类型:需要两段内存 第一段存储实际的数据 它在堆中储存。第二段 是一个应用 指针 ,指向数据在堆内存中储存数据的位置 地址储存在栈中,通过栈中的地址快速的找到堆中的数据,对象与对象之间不会相互影响 通过new出来的对象都会在堆内存中开辟一块新的空间
- 静态资源区:对于字符串常量来说 实际是储存在静态资源区的 即使在堆中用的字符串也是存放在静态资源区的 我如果用到重复的字符串时,他们指向的地址是一样的 会解决内存问题 字符串在静态资源区中是不可以改变的 ,看到的修改其实只是修改指针指向的位置 内存并没有修改
三、继承与虚方法(virtual)
定义父类与虚方法:
internal class Boss
{
public int HP { get; set; }
public int SP { get; set; }
//virtual 虚方法的关键字 将方法升级为虚方法 然后就可以在子类中重写逻辑了 在子类中重写之后就属于子类逻辑
public virtual void Move()
{
Console.WriteLine("Boss只能左右移动");
}
public void AI()
{
Console.WriteLine("大招");
}
}
定义子类并重写:
internal class xiao1:Boss
{ //override 重写关键字
//如果想要重写父类中的某个逻辑 那么父类中的方法就可以升级为虚方法,然后在子类中对其进行重写
//如果父类中的方法没有声明为虚方法,子类就不能重写
public override void Move()
{
Console.WriteLine("小怪可以任意移动");
}
//隐藏方法
public new void AI()
{
Console.WriteLine("大招");
}
}
四、只读字段readonly
如图变量name只可以读取不可以修改值。
public readonly static string name;
五、日期与时间(DateTime)
//获取当前时间
//当前计算机的时间
DateTime dl = DateTime.Now;
//string time = dl.ToString();
//Console.WriteLine(time);
Console.WriteLine(dl);
// 如何设置一个时间
DateTime dl1 = new DateTime(2024, 7, 10, 20, 5, 20);
Console.WriteLine(dl);
//年 Year
Console.WriteLine(dl.Year);
// Month 月
Console.WriteLine(dl.Month);
//Console.WriteLine(dl1.Month);
// Day 日
Console.WriteLine(dl.Day);
// Hour 时
Console.WriteLine(dl.Hour);
// Minute 分
Console.WriteLine(dl.Minute);
// Second 秒
Console.WriteLine(dl.Second);
// 获取当前时间 在本年度中过去了多少天
Console.WriteLine(dl.DayOfYear);
Console.WriteLine(dl.TimeOfDay); // 当前时间的小时
Console.WriteLine(dl.DayOfWeek); // 当前时间是周几
DateTime d = dl.AddDays(100); // 在当前时间上加某天数后的时间
DateTime d1 = dl.AddDays(-100);
Console.WriteLine(d);
DateTime d2 = dl.AddMinutes(100);
Console.WriteLine(d2);
//TimeSpan 时间长度 5天12小时39分钟50秒
//TimeSpan t1 = d - d2;
//Console.WriteLine(t1);
TimeSpan t2 = DateTime.Now.Subtract(new DateTime(1997, 5, 14, 17, 30, 25));
Console.WriteLine(t2);
// 获取时间长度中的天数
Console.WriteLine(t2.Days);
//总天数
Console.WriteLine(t2.TotalDays);
// 小时
Console.WriteLine(t2.Hours);
//总小时
Console.WriteLine(t2.TotalHours);
//分
//秒