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

正则表达式 - 双引号内字符转星号/单词首字母转大写

发布时间:2020-12-14 02:06:56 所属栏目:百科 来源:网络整理
导读:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Text.RegularExpressions;//引用namespace WindowsFor
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;//引用

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnChange_Click(object sender,EventArgs e)
        {
            //首字母大写
            //rtbResult.Text = Regex.Replace(rtbSource.Text,@"w+",new MatchEvaluator(CapText));
            //双引号内其他字符转星号
            rtbResult.Text = Regex.Replace(rtbSource.Text,""[^"]*"",new MatchEvaluator(XingHao));

        }

        //将单词的首字母转为大写(注:此处引用自 http://msdn.microsoft.com/zh-cn/library/cft8645c(v=vs.80).aspx)
        //如:【may i help you】 转为:【May I Help You】
        static string CapText(Match m)
        {
            // Get the matched string.
            string x = m.ToString();
            // If the first char is lower case...
            if (char.IsLower(x[0]))
            {
                // Capitalize it.
                return char.ToUpper(x[0]) + x.Substring(1,x.Length - 1);
            }
            return x;
        }

        //将双引号中的字符转为星号* 
        //如:【"what" can "i" do for you"】 转为:【"****" can "*" do for you"】
        static string XingHao(Match m)   //本人仍觉此算法略显粗糙,有较好算法者望指教一二
        {
            StringBuilder strBld = new StringBuilder();
            string x = m.ToString();

            for (int i = 0; i < x.Length - 2;i++ )
            {
                strBld.Append("*");
            }
            
            return x.Substring(0,1) + strBld.ToString() + x.Substring(x.Length-1);
        }

    }

}

(编辑:李大同)

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

    推荐文章
      热点阅读