11---Net基础加强

替换邮箱用户名部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Net;

namespace _01替换邮箱用户名部分
{
    class Program
    {
        static void Main(string[] args)
        {
           
            #region MyRegion

            //string msg = "我的邮箱是zxh@itcast.cn杨中科的邮箱是yzk365@chezhihui.com减肥看到了撒steve_zhao@163.com发动机是啦";         
            ////msg = Regex.Replace(msg, @"([-a-zA-Z0-9_.]+)(@[-a-zA-Z0-9]+(.[a-zA-Z0-9]+){1,})", "***********");
            ////msg = Regex.Replace(msg, @"([-a-zA-Z0-9_.]+)(@[-a-zA-Z0-9]+(.[a-zA-Z0-9]+){1,})", "****$2");
            ////通过委托实现的字符串替换
            //msg = Regex.Replace(msg, @"([-a-zA-Z0-9_.]+)(@[-a-zA-Z0-9]+(.[a-zA-Z0-9]+){1,})", GetReplaceString);
            //Console.WriteLine(msg);
            //Console.Read();   
     
            #endregion

            #region MyRegion

            ////WebClient类的主要目的就是从网络上上传或下载内容
            //WebClient wc = new WebClient();
            ////WebRequest
            ////  WebResponse
            //wc.DownloadFile("http://fdsfdsfdsfdsfasdf", @"C:UsersAdministratorDesktop图片");
            //wc.DownloadString();
            //wc.DownloadData();
            ////wc.UploadData();
            ////wc.UploadFile();
            ////wc.UploadString();
            //Console.WriteLine("OK");
            //Console.Read();  

            #endregion

        }

        public static string GetReplaceString(Match match)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < match.Groups[1].Length; i++)
            {
                sb.Append("*");
            }
            sb.Append(match.Groups[2].Value);
            return sb.ToString();
        }

    }
}

单词边界断言 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace 单词边界断言
{
    class Program
    {
        static void Main(string[] args)
        {

            //string msg = "The The day after tomorrow is my wedding day.The row, we are looking for is .row. number 10.";

            //// 就表示单词的边界,什么是单词的边界,
            ////单词的边界表示:一边是单词,一边不是单词,这个就是单词的边界。
            ////  表示的是一个断言,什么是断言:只验证,不匹配。
            //msg = Regex.Replace(msg, @"row", "line");
            //Console.WriteLine(msg);
            //Console.Read();


            #region 练习:Hi,how are you?Welcome to our country!请提取出3个字母的单词。

            ////D-Day
            ////VE-Day

            ////单词:w  (0-9a-zA-Z_)
            ////环视,断言。
            ////  wW    Ww
            //string msg = "Hi,how 12are12 you?Welcome to our country!";
            //MatchCollection matches = Regex.Matches(msg, @"[a-zA-Z]{3}");
            //foreach (Match item in matches)
            //{
            //    Console.WriteLine(item.Value);
            //}
            //Console.ReadKey();

            #endregion

            #region MyRegion

            //注意:必须是一边是单词,一边不是单词。这里所说的单词就是指的 w  所以为空
            string msg = "# ## ### #### ## ### # ###.";
            MatchCollection matches = Regex.Matches(msg, @"###");
            foreach (Match item in matches)
            {
                Console.WriteLine(item.Value);
            }
            Console.Write("ok");
            Console.ReadKey();

            #endregion



        }
    }
}

反向引用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;

namespace _03反向引用
{
    class Program
    {
        static void Main(string[] args)
        {
            #region MyRegion

            ////1.将”杨杨杨杨杨中中中中科科科科科”变成”杨中科”。
            ////string msg = "杨杨杨杨杨中中中中科科科科科";
            //string msg = "杨杨杨杨杨中中中中科科科科科AAAAAAAAAAABBBBBBBBBBBBC CCCCCCCCCCCCCCC啊啊啊啊啊啊啊啊啊啊啊买买买嘎嘎嘎嘎嘎得得得得";
            ////这个就是反向引用,在正则表达式内部,要引用内部的分组,则使用1   2   3  4 等来引用分组。
            //msg = Regex.Replace(msg, @"(.)1+", "$1");
            //Console.WriteLine(msg);
            //Console.Read();


            ////2.将"我...我我..我我我我....爱爱爱..爱..爱...你你...你..你你你...",变成"我爱你"
            //string msg = "我...我我..我我我我....爱爱爱..爱..爱...你你...你..你你你...";
            //msg = msg.Replace(".", string.Empty);
            //msg = Regex.Replace(msg, @"(.)1+", "$1");
            //Console.WriteLine(msg);
            //Console.Read();

            //string msg = "我...我我..我我我我....爱爱爱..爱..爱...你你...你..你你你...";
            //MatchCollection match = Regex.Matches(msg, @"((.)2+.+)+");
            //foreach (Match item in match)
            //{
            //    Console.WriteLine(item.Value);
            //}
            //Console.WriteLine("共找到了:{0}个匹配。", match.Count);
            //Console.Read();

            #endregion


            #region MyRegion

            //string filetxt = File.ReadAllText("英汉词典TXT格式.txt", Encoding.GetEncoding("gb2312"));
            //MatchCollection matches = Regex.Matches(filetxt, @"[a-zA-Z]*([a-zA-Z])1+[a-zA-Z]*");
            //using (StreamWriter writer = new StreamWriter("words.txt"))
            //{
            //    foreach (Match item in matches)
            //    {
            //        //Console.WriteLine(item.Value);
            //        writer.WriteLine(item.Value);
            //    }
            //}
            //Console.WriteLine(matches.Count);
            //Console.WriteLine("ok");

            //Console.Read();

            #endregion

        }
    }
}

ubb替换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace _04UBB翻译
{
    class Program
    {
        static void Main(string[] args)
        {

              ////把ubb代码翻译成了html代码
            ////<b>加粗的字体</b>
            //string msg = "[b]加粗的字体[/b]哈哈哈哈[b]传智播客[/b][b]加粗的字体[/b]哈哈哈哈[b]传智播客[/b]";
            //msg = Regex.Replace(msg, @"[b](.+?)[/b]", "<b>$1</b>");
            //Console.WriteLine(msg);
            //Console.Read();

            //[b]新网站[/b]    <b>新网站</b>
            //[url=http://www.qq.com]秋秋[/url]  <a href="http://www.qq.com">秋秋</a>

            string ubb = "【你好,我发现一个[b]新网站[/b],[b]大家[/b]来看呀[url=http://www.qq.com]秋秋[/url],另外一个有时间也可以看看[url=http://www.rupeng.com]如鹏[/url],还有[url=http://www.itcast.cn]传智播客[/url]】。吼吼!";

            //替换b标签
            string html = Regex.Replace(ubb, @"[b](.+?)[/b]", "<b>$1</b>", RegexOptions.IgnoreCase);

            //替换url标签  url=http://www.qq.com]秋秋[/url]
            html = Regex.Replace(html, @"[url=(.+?)](.+?)[/url]", "<a href="$1">$2</a>", RegexOptions.IgnoreCase);

            //<a href=".+">.+</a>

            Console.WriteLine(html);
            Console.Read();

        }
    }
}

敏感词过滤

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;


namespace _05敏感词过滤
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取用户输入
            string userInput = txtWords.Text.Trim();

            //用来存储 “严禁发帖”的词汇
            StringBuilder sbBanned = new StringBuilder();

            //用来存储 “需要人工审核的词汇”
            StringBuilder sbMode = new StringBuilder();


            string[] lines = File.ReadAllLines("网站过滤词(部分).txt", Encoding.Default);
            for (int i = 0; i < lines.Length; i++)
            {
                string[] parts = lines[i].Split('=');
                if (parts[1] == "{MOD}")
                {
                    sbMode.Append(parts[0] + "|");
                }
                else if (parts[1] == "{BANNED}")
                {
                    sbBanned.Append(parts[0] + "|");
                }
            }
            sbMode.Remove(sbMode.Length - 1, 1);
            sbBanned.Remove(sbBanned.Length - 1, 1);


            //1.检查是否包含严禁发帖的词汇
            if (Regex.IsMatch(userInput, sbBanned.ToString()))
            {
                MessageBox.Show("包含禁止发帖词汇!");
            }
            else if (Regex.IsMatch(userInput, sbMode.ToString()))
            {
                MessageBox.Show("需要人工审核");
            }
            else
            {
                MessageBox.Show("可以发帖1!!");
            }

            //2.检查是否包含需要人工审核的词汇

        }
    }
}

委托理解1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ClassLibrary1
{
    public class Class1
    {
        //第二步:通过委托类型,声明一个委托变量。
        public T1Delegate method;
        public void Do()
        {
            Console.WriteLine("=====================");
            Console.WriteLine("=====================");
            //因为委托是引用类型而引用的默认值都是null,所以在使用委托变量之前要进行非空校验
            if(method!=null)
            {
            //这个委托变量method里面将来存储的就是一个方法,所以可以直接调用该变量,就相当于是调用了里面的方法
                method();
            }
            //T1();
            Console.WriteLine("=====================");
            Console.WriteLine("=====================");
        }

        public void T1()
        {
            //Console.WriteLine("这是个T1方法,当前时间为:{0}",DateTime.Now.ToString());
            File.WriteAllText("datetime.txt",DateTime.Now.ToString());
            Console.WriteLine("时间写入完毕!");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
      //第一步定义委托
      //定义了委托
     //这里就像定义了一个Person类一样,将来要用的时候还得声明该类型的一个变量才能使用
 
     public  delegate void T1Delegate();

     public delegate void T2Delegate(int n);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06委托介绍
{
    class Program
    {
        static void Main(string[] args)
        {
            ClassLibrary1.Class1 c1 = new ClassLibrary1.Class1();
            //把M1方法赋值给了委托变量method,这样method就可以使用了,并且里面存储的就是M1方法
            c1.method = M1;
            c1.Do();
            Console.ReadKey();
        }

        static void M1()
        {
            Console.WriteLine("当前时间为:{0}",DateTime.Now.ToString());
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace _07委托介绍
{
    class Program

    { 
        static void Main(string[] args)
        {
            ClassLibrary1.Class1 c1 = new ClassLibrary1.Class1();
            //把M1方法赋值给了委托变量method,这样method就可以使用了,并且里面存储的就是M1方法
            c1.method = M2;
            c1.Do();
            Console.ReadKey();
        }

        static void M2()
        {
            File.WriteAllText("datetime.txt", DateTime.Now.ToString());
            Console.WriteLine("时间写入完毕!");
        }
     }
  
}

委托举例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary2
{
      public delegate string ModifyStringDelegate(string str);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary2
{
    public class Class1
    {
        /// <summary>
        /// 修改字符串数组中的每个元素的内容
        /// </summary>
        /// <param name="array"></param>
        public void ChangeStrings(string[] array,ModifyStringDelegate method)
        {
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = method(array[i]);
            }
        
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary2;

namespace Wt调用者1
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            string[] names = new string[] { "John","Bob","Chris","Steve","Johnny"};
            c1.ChangeStrings(names,M1);
            foreach (var item in names)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }

        static string M1(string element)
        {
            return "" + element + "";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary2;

namespace Wt调用者2
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            string[] names = new string[] { "John", "Bob", "Chris", "Steve", "Johnny" };
            c1.ChangeStrings(names, M2);
            foreach (var item in names)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }

        static string M2(string msg)
        {
            return msg.ToUpper();
        }
    }
}

通过委托实现窗体回传值

效果图:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 委托案例
{
 public delegate void UpdateTextBoxDelegate(string msg);
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 委托案例
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //按钮的单击事件,当点击按钮的时候弹出窗体2
        private void button1_Click(object sender, EventArgs e)
        {
            string content = textBox1.Text.Trim();

            //创建一个窗体2的对象,即创建Form2的对象
            Form2 f2 = new Form2(content,UpdateTextbox);
            f2.Show();
        }

        private void UpdateTextbox(string s)
        {
            this.textBox1.Text = s;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 委托案例
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(string msg,UpdateTextBoxDelegate method):this()
        {
            //设置窗体2的label显示窗体1传递过来的字符串
            label1.Text=msg;
            this._update = method;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private UpdateTextBoxDelegate _update;
        private void button1_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text.Trim();
            if(_update!=null)
            {
                this._update(str);
            }
            this.Close();
        }
    }
}

委托-匿名方法-lambda表达式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 匿名方法
{
    class Program
    {
        static void Main(string[] args)
        {

            #region MyRegion

            ////委托类型变量只能赋值委托对象,看起来把一个方法赋值给了一个委托变量,
            ////其实内部也帮我们创建了一个委托对象,把方法作为委托参数传进去
            //Delegate1 md = M1;
            ////Delegate1 md = new Delegate1(M1);
            ////md();
            //md.Invoke();//等价 md();
            //Console.WriteLine("OK");
            //Console.ReadKey();

            ////委托其实编译的时候就是编译成了一个类。
            ////并且这个类继承自MulticastDelegate,并且这个类又继承自Deletegate,
            ////所以说Deletegate是所有委托的祖宗类

            #endregion

            #region MyRegion

            ////带两个参数,带一个返回值的委托
            //Delegate3 md = Add;
            //int n=md(10,20);
            //Console.WriteLine(n);
            //Console.ReadKey();

            #endregion

            #region 匿名方法与lamada表达式

            //匿名方法

            ////无参数我返回值的写法
            //Delegate1 md = delegate() 
            //{ 
            //    Console.WriteLine("我是一个匿名方法"); 
            //};
            //md();

            ////lambda
            //Delegate1 md = () => { Console.WriteLine("我就是一个匿名函数"); };

            ////有一个参数有一个返回值的写法
            ////Delegate4 md = M2;
            ////int x=md(10);
            //Delegate4 md = delegate(int c)
            //{
            //    return c * 2;
            //};
            //int x = md(10);
            //Console.WriteLine(x);
            //Console.ReadKey();

            //Delegate4 md = (x) => { return x * 2; };
            //int result = md(10);
            //Console.WriteLine(result);
            //Console.ReadKey();

            //Delegate4 md = x => x*2;
            //int result = md(10);
            //Console.WriteLine(result);
            //Console.ReadKey();

            ////有两个参数有一个返回值的写法
            //Delegate3 md = delegate(int n,int m)
            //{
            //    return n + m;
            //};
            //int x = md(100,200);
            //Console.WriteLine(x);
            //Console.ReadKey();

            //Delegate3 md = (x,y) => x+y;
            //int result = md(100,200);
            //Console.WriteLine(result);
            //Console.ReadKey();

            //List<int> list = new List<int>() {1,2,3,4,5,6,7,8,9,10 };
            //foreach (var item in list.Where(x => x > 5))
            //{
            //    Console.WriteLine(item);
            //}
            //Console.ReadKey();

            #endregion
           
        }

        static void M1()
        {
            Console.WriteLine("M1方法");
        }

        static int M2(int n)
        {
            return n*2;
        }

        static int Add(int n1,int n2)
        {
            return n1 + n2;
        }

        //要保存这个方法,我们可以再写一个委托来实现。
        static int Add(int n1, int n2,int n3)
        {
            return n1 + n2+n3;
        }

    }

    public delegate void Delegate1();

    public delegate void Delegate2(int n);

    public delegate int Delegate3(int n,int m);

    public delegate int Delegate4(int n);

}
原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/4157508.html