首页 > 编程 > C# > 正文

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

2019-10-29 21:36:39
字体:
来源:转载
供稿:网友

这篇文章主要介绍了C#使用正则表达式实现首字母转大写的方法,涉及C#基于正则表达式操作字符串的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

 

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

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


注:相关教程知识阅读请移步到c#教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表