要通过表达式直接实现。

解决方案 »

  1.   

    reg=/.../;
    var v=reg.exec(str);
    v=v.toUpperCase();
      

  2.   

    这个是用了JS里的方法。
    现在的需求是将部分字符大写。比如首字大写、其余小写。通过它自身的replace功能实现。
      

  3.   

    就比如“一句英文,要求各首字母大写、其余小写”。用正则表达式实现。
    现在我用的是表达式结合C#来,还要得到个matchsCollection再逐个toUpper。麻烦,也有失精准。
      

  4.   

    string a="hello chaney";
    a=Regex.Replace(a,"^.",a.Substring(0,1).ToUpper());
      

  5.   

    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 + "]");
          string result = Regex.Replace(text, @"\w+",
             new MatchEvaluator(RegExSample.CapText));
          System.Console.WriteLine("result=[" + result + "]");
       }
    }
    MSDN 上有现成的例子
      

  6.   

    呵。其实我的想法就是通过写表达式实现。现在自己用的办法是:
    模板为:匹配第一个字符及前面一个字符为非字母的字符,
    则得到一个matchCollection.然后循环,替换。我觉得这样效率不高,也没体现正则表达式的方便,所以想寻求更适合的方法。hatita(悠远的风景) 的方法中:
    new MatchEvaluator(RegExSample.CapText));
    这里没看懂,怎么RegExSample.CapText没传值呢?或者MatchEvaluator可以自动处理了?