加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式替换---lamda表达式(UniCode编码)

发布时间:2020-12-14 02:27:54 所属栏目:百科 来源:网络整理
导读:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace RegexReplaceTest{ class Program { static void Main(string[] args) { string text = "疲惫的时候,可以哼唱一支歌
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexReplaceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "疲惫的时候,可以哼唱一支歌,寻找自由奔放的感觉";
            Regex reg = new Regex(".");
            Console.WriteLine(text);
            Console.WriteLine("UniCode编码");
            string result = reg.Replace(text,m =>"&#"+Convert.ToString((int)(m.Value.ToCharArray().First()),10).ToString()+"; ");
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

结果:“疲 惫 的 时 候 , 可 以 哼 唱 一 支 歌 , 寻 找 自 由 奔 放 的 感 觉 ”

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

namespace RegexReplaceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "疲惫的时候,可以哼唱一支歌,寻找自由奔放的感觉";
            Regex regEncode = new Regex(".");
            Regex regDecode = new Regex("&#(.+?);");
            Console.WriteLine(text);
            Console.WriteLine("UniCode编码:");

            string result = regEncode.Replace(text,m => "&#" + Convert.ToString((int)(m.Value.ToCharArray().First()),10).ToString() + ";");
            Console.WriteLine(result);

            Console.WriteLine("UniCode解码:");
            result = regDecode.Replace(result,m => ((char)Convert.ToInt32(m.Groups[1].Value,10)).ToString());
            Console.WriteLine(result);

            Console.Read();
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读