C#使用正则表达式实现首字母转大写的方法

2019-12-26 14:07:20王振洲

易采站长站为您分析C#使用正则表达式实现首字母转大写的方法,涉及C#基于正则表达式操作字符串的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#使用正则表达式实现首字母转大写的方法。,具体如下:

 

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

希望本文所述对大家C#程序设计有所帮助。


注:相关教程知识阅读请移步到c#教程频道。