c# 正则表达式 首字母转大写


 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             // Input strings.
 6             const string s1 = "samuel allen";
 7             const string s2 = "dot net perls";
 8             const string s3 = "Uppercase first letters of all words in the string.";
 9 
10             // Write output strings.
11             Console.WriteLine(TextTools.UpperFirst(s1));
12             Console.WriteLine(TextTools.UpperFirst(s2));
13             Console.WriteLine(TextTools.UpperFirst(s3));
14             Console.ReadKey();
15         }
16     }
17 
18     public static class TextTools
19     {
20         /// <summary>
21         /// Uppercase first letters of all words in the string.
22         /// </summary>
23         public static string UpperFirst(string s)
24         {
25             return Regex.Replace(s, @"\b[a-z]\w+", delegate(Match match)
26             {
27                 string v = match.ToString();
28                 return char.ToUpper(v[0]) + v.Substring(1);
29             });
30         }
31     }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM