现在的位置: 首页 > 综合 > 正文

匹配所有单词首字母大写

2012年12月15日 ⁄ 综合 ⁄ 共 615字 ⁄ 字号 评论关闭
using System;
using System.Text.RegularExpressions;

class RegExSample
{
    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;
    }

    static void Main()
    {
        string text = "four score and seven years ago";

        System.Console.WriteLine("text=[" + text + "]");

        Regex rx = new Regex(@"\w+");

        string result = rx.Replace(text, new MatchEvaluator(RegExSample.CapText));

        System.Console.WriteLine("result=[" + result + "]");
    }
}

抱歉!评论已关闭.