StingBuilder操作
Append("")方法,在字符串尾部添加字符串
ToString()得到字符串。
Insert(,)在指定索引位置插入指定字符串
Remove()移除指定区域的字符串
Replace()指定区域字符串替换成指定字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn
{
class A
{
static void Main(string[] args)
{
//StringBuilder stringBuilder_1 = new StringBuilder("www.touming.xyz");
//StringBuilder stringBuilder_2 = new StringBuilder(20);
StringBuilder num = new StringBuilder("www.touming", 30);
num.Append(".xyz");
Console.WriteLine(num.ToString());
num.Insert(0, "http://");
Console.WriteLine(num.ToString());
num.Replace("http://", "https://");
Console.WriteLine(num.ToString());
num.Remove(0, 7);
Console.WriteLine(num.ToString());
}
}
}
正则表达式

定位元字符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace learn
{
class A
{
static void Main(string[] args)
{
string str = "touming";
str = Regex.Replace(str, "^", "www.");
str = Regex.Replace(str, "$", ".xyz");
//$表示字符串结尾
//^表示字符串开头
//Regex类必须包含System.Text.RegularExpressions名称空间
Console.WriteLine(str);
}
}
}
^匹配字符串的开头。
$匹配字符串的结尾。
基本语法元字符

Regex.IsMatch()方法,用于判断一个字符串是否符合对应的正则表达式
正则表达式也是string类
"^\d"表示以数字开头,*代表0个或多个元字符,定位元字符会自己去寻找最近的元字符
"^\d*$"以数字开头结尾,中间字符也是数字
为了保证元字符的\不被判断为转义字符,需要在""前加@
委托
如果我们要吧方法当作参数来传递的话,就要用到委托
简单来说委托就是一个类型
这个类型可以赋值一个方法的引用
delegate 返回值类型 委托名称(传递参数声明);
声明委托变量时,传递一个方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn
{
class Program
{
//委托
/*
* 如果我们要吧方法当作参数来传递的话,就要用到委托
* 简单来说委托就是一个类型
* 这个类型可以赋值一个方法的引用
*/
private delegate string GetString();//定义一个委托
//private delegate int Get();
static void Main(string[] args)
{
int a = 20;
GetString A_getString = new GetString(a.ToString);
//声明一个委托变量,且构造函数里传递的是一个方法
//这里可以理解为A_getString指向了a.ToString
Console.WriteLine(A_getString());//直接调用方法
//int num_1 = Convert.ToInt32(Console.ReadLine());
//int num_2 = Convert.ToInt32(Console.ReadLine());
}
//public int Add(int a,int b)
//{
// return a + b;
//}
}
}
GetString A_getString = new GetString(a.ToString);
也可以写成
GetString A_getString = a.ToString;
直接通过赋值的方法。
使用委托的自定义方法必须是静态的 static
调用方法可以传递声明的委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn
{
class Program
{
private delegate int GetAdd(int a, int b);
static void Main(string[] args)
{
GetAdd add = Add;
Print(add, 2, 3);
}
static int Add(int a, int b)
{
return a + b;
}
static void Print(GetAdd get, int a, int b)
{
int ret = get(a, b);
Console.WriteLine(ret);
}
}
}










