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 }
工欲善其事,必先利其器。