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

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

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值