C#语言笔记-10

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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值