10、c#方法参数+正则表达式
·
1 参数传递
/*
01:::方法的参数传递
定义时 参数列表时形参::定义的变量
调用时 传递的数据是实参:::运行方法需要的原始数据
*/
static void Main(string[] args)
{
int aa = 1;
test11(aa);//把main中的变量aa的值 复制一份 给我了test1中的变量a
//方法就是代码块:使用代码块替代方法
//int a=aa; a++;
//值传递::基本数据类型作为参数 传递的是值 把实参的值复制一份给了形参 在方法中只能更改形参的值
Console.WriteLine("aa="+aa);
int[] arr = { 1,2,3,4};
test12(arr);//更改为代码块::int[] a=arr; main中的引用arr和test12中的引用a指向同一个对象27的数组对象
Console.WriteLine("arr[0]=" + arr[0]);
//引用传递::引用数据类型作为参数 传递的是内存地址 形参和实参对应的是同一个对象
}
static void test11(int a) {
a++;
}
static void test12(int[] a) {
// a = new int[] { };
a[0]++;
}
2 参数类型
int b = 1;
test01(b);
Console.WriteLine("值参数::"+b);//实参的值赋值一份给形参
int[] arr = { 1,2,2,3};
test02(arr);//实参arr和形参a指向同一个对象 18行对象
//ref参数::形参和实参绑定了::不管是基本数类型还是引用数据类型 对形参的所有操作都会影响实参
b = 1;
test031(ref b);//ref参数 传递实参是 也需要声明一下ref
Console.WriteLine("值参数::" + b);//实参的值赋值一份给形参
arr = new int[] { 1, 2, 2, 3 };
test032(ref arr);//实参arr和形参a指向同一个对象 18行对象
Console.WriteLine("arr[0]=" + arr[0]);
//out参数 等价于返回值 实现 通过形参给实参赋值
int c;
test04(out c);
int f = test04(out c);
Console.WriteLine("c="+c);
//命名参数:传递实参时 声明形参的名字 可以实现参数顺序可变
test05(1,1.1,true);
test05(c:true,a:1,b:1.1);
//可选参数:可以选择不给参数赋值 参数有默认值
test06(1,1);
test06(1);
//不定参数:params:: static void test07(double b, params int[] a)
test07(1.1,1,1,1,1,12,2,3,3);
test07(1.1);
}
//正常参数1:值参数
static void test01(int a) {
a++;//更改形参的值 对实参无法有印象
}
//正常参数1:引用参数
static void test02(int[] a)
{
a[0]++;//形参和实参指向的是同一个对象
}
//ref:::把值传递转化为引用传递
static void test031(ref int a)
{
a++;//更改形参的值 对实参无法有印象
}
//ref参数
static void test032(ref int[] a)
{
//a[0]++;//更改形参的值 对实参无法有印象
a = new int[] { -1,-2};
}
//out参数
static int test04(out int a)
{
a = 10;
return 0;
}
//命名参数:
static void test05(int a,double b,bool c)
{
Console.WriteLine("a="+a+",b="+b+",c="+c);
}
//可选参数
static void test06(double b, int a=1)
{
Console.WriteLine("a=" + a + ",b=" + b );
}
//不定参数
static void test07(double b, params int[] a)
{
Console.WriteLine("a=" + a + ",b=" + b);
}
3 正则表达式
3.1 概念
给字符串定义正确规则的字符串式子
3.2 感受一下
static void Main(string[] args)
{
Random random = new Random();
for (int i = 0; i < 100; i++) {
string num = random.Next(1000000,9999999)*1L*10000+"";
Console.WriteLine(num + ":::" + isPhone01(num));
Console.WriteLine(num + ":::" + isPhone02(num));
Console.WriteLine();
}
}
//判断参数字符串是不是手机号
public static bool isPhone01(string phone) {
if (phone.Length!=11) {
return false;
}
if (phone[0]!='1') {
return false;
}
//3-9
if (phone[1]<'3' || phone[1]>'9') {
return false;
}
//其他字符必须是数字字符
for (int i = 2; i < phone.Length; i++) {
if (phone[i] < '0' || phone[i] > '9')
{
return false;
}
}
return true;
}
public static bool isPhone02(string phone)
{
return Regex.IsMatch(phone, "1[3-9][0-9]{9}");
}
-
总结
1 正则表达式 使用的是Regex类的静态方法
2 表示正则表达式的字符串::使用特殊字符表示一些特殊含义:
[]取值范围
{}次数
中括号中的-表示到
3 使用正则表达式:简化代码优点::可读性差
4 正则表达式中的特殊字符含义
4.1 表示字符
. :::表示任意字符 x :::x是啥 就表示啥字符 1 a b 苗 \d:::数字字符 \D:::非数字字符 \s:::空白字符::空格 换行符 制表符 \S:::非空白字符 \w:::单词字符 \W:::非单词字符 $:::表示字符串结束 ^:::表示字符串开头 \u4e00-\u9fa5 表示汉字
4.2 表示取值范围
[] 表示范围 [abc] 可以取值a或者b 或者 c [a-z] 可以取值编码表中a到z [a-z[A-Z]] 并集 [a-g&&[b-Z]] 交集 [^a-z] 除了a-z的其他所有字符
4.3 表示次数
{} 表示次数
{n} 出现n次数
{n,} 出现>=n次
{n,m} 出现>=n次 <=m次
x+ x出现>=1次
x? x出现<=1次
x* x出现>=0次
4.4 第一次感受正则
static void Main(string[] args)
{
Random random = new Random();
for (int i = 0; i < 100; i++) {
string num = random.Next(1000000,9999999)*1L*10000+"";
// Console.WriteLine(num + ":::" + isPhone01(num));
// Console.WriteLine(num + ":::" + isPhone02(num));
// Console.WriteLine();
}
}
//判断参数字符串是不是手机号
public static bool isPhone01(string phone) {
if (phone.Length!=11) {
return false;
}
if (phone[0]!='1') {
return false;
}
//3-9
if (phone[1]<'3' || phone[1]>'9') {
return false;
}
//其他字符必须是数字字符
for (int i = 2; i < phone.Length; i++) {
if (phone[i] < '0' || phone[i] > '9')
{
return false;
}
}
return true;
}
public static bool isPhone02(string phone)
{
return Regex.IsMatch(phone, "1[3-9][0-9]{9}");
}
4.5 IsMatch;判断整体是否匹配
//Regex类的方法1:::bool isMatch(str,pattern):::判断整体
public static void RegexMethod01() {
// \\.表示字符.
// . 表示任意字符
//判断邮箱
Console.WriteLine("ismatch:"+(Regex.IsMatch("miaotianbao@163.com", "^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\\.[a-zA-Z]+$")));
Console.WriteLine("ismatch:"+(Regex.IsMatch("552332689@qq.com", "^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\\.[a-zA-Z]+$")));
Console.WriteLine("ismatch:"+(Regex.IsMatch("1@1.com", "^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\\.[a-zA-Z]+$")));
//判断是不是人名字::2-4中文
Console.WriteLine("ismatch:" + (Regex.IsMatch("苗天保", "^[\u4e00-\u9fa5]{2,4}$")));
Console.WriteLine("ismatch:" + (Regex.IsMatch("tom", "^[\u4e00-\u9fa5]{2,4}$")));
Console.WriteLine("ismatch:" + (Regex.IsMatch("苗天11", "^[\u4e00-\u9fa5]{2,4}$")));
}
4.6 Matchs::获取所有匹配的子串
//Regex类的方法2:::MatchCollection Matches(str,pattern):::获取匹配的所有子串
public static void RegexMethod02() {
MatchCollection result = Regex.Matches("1bac123abc987呵呵呵985-sbc287", "[0-9]+");
for (int i = 0; i < result.Count; i++)
{
Console.WriteLine(result[i].Value);
}
//获取其中非空格的子串
result = Regex.Matches("abc 1982 sdhdh sheh\t10192\r\n101992sjsjs 1111", "\\S+");
for (int i = 0; i < result.Count; i++)
{
Console.WriteLine(":::" + result[i].Value);
}
result = Regex.Matches("15036088030苗天保18860388009mtb13838106769张三", "1[3-9][0-9]{9}");
for (int i = 0; i < result.Count; i++)
{
Console.WriteLine(":::::::" + result[i].Value);
}
}
4.7 Split::切割
//Regex类的方法3:::string[] split(str,pattern):::切割
public static void RegexMethod03()
{
//使用;!。切割
string[] arr = Regex.Split("abc;123;olk;;;12p;098,111!987。111","[;,。!]+");
//使用叠词切割
arr = Regex.Split("abc11dfg88klooo4567hh87米欧8777902", "(.)\\1+");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(i + ":::::::" + arr[i]);
}
}
4.8 Replace::替换
//Regex类的方法4:::string replace(str,pattern):::替换
public static void RegexMethod04()
{
string str = Regex.Replace("abc;123;olk;;;12p;098,111!987。111", "[;,。!]+","");
//去除叠词::11122223333318887722---1231872
//1 表示1 字符 \\1 表示组1
//第三个参数 $1 表示用组1的字符替换
//(.)\\1+ 叠词的规范写法
str = Regex.Replace("11122223333318887722", "(.)\\1+", "$1");
Console.WriteLine("替换后的字符串:"+str);
//替换格式为 aba 替换为b
str = Regex.Replace("1218979bklkdcdfhjkj", "(.)(.)\\1", "$2");
Console.WriteLine("替换后的字符串:" + str);
Regex.IsMatch("",@"\d");
}
5 转义字符\
/*
\转义字符:用于转换字符的含义
::把特殊字符转化为普通字符
::把普通字符转换为特殊字符
" ' \在字符串中有特殊含义:::表示字符和字符串的结尾
*/
Console.WriteLine('\"');//表示普通字符"
Console.WriteLine('\\');//表示普通字符\
Console.WriteLine('\t');//表示制表符
Console.WriteLine('\n');
Console.WriteLine(@"\");//加个@表示所有字符都是普通字符 不转意
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)