老外编的程序(三)--正则表达式的Demo

发表于:2007-06-30来源:作者:点击数: 标签:
using System.Text.RegularExpressions; class Test { 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]) +
using System.Text.RegularExpressions;

class Test {
    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 + "]");
    string result = Regex.Replace(text, @"w+",
                      new MatchEvaluator(Test.CapText));
    System.Console.WriteLine("result=[" + result + "]");
    }
}

原文转自:http://www.ltesting.net