码迷,mamicode.com
首页 > Windows程序 > 详细

C# 将字符串首字母变为大写

时间:2017-07-01 11:56:47      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:null   log   ret   title   class   res   rar   时间   中间   

  不太会写文章,大概就如题吧。不多说,直接贴代码。

  

技术分享
 1         public static string TitleToUpper(this string str)
 2         {
 3             if(string.IsNullOrWhiteSpace(str))
 4                 return string.Empty;
 5 
 6             char[] s = str.ToCharArray();
 7             char c = s[0];
 8 
 9             if(a <= c && c <= z)
10                 c = (char)(c & ~0x20);
11 
12             s[0] = c;
13 
14             return new string(s);
15         }    
字符串首字母大写

  因为具体问题,这里没有判断字符串是否为纯英文,如果有需要可以在中间加一句。我想到的是正则表达式,按照微软的惯例,应该是用编码的方式去查询是否为英文。

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^[A-Za-z0-9]+$");  

  大概就这样吧。感觉如果写了一段时间代码的人,对于处理字符串还在用各种SubString(不是说不能用,而是应该尽量避免用,这个函数真的很不友好)然后各种拼接的话,真的有点Low。

C# 将字符串首字母变为大写

标签:null   log   ret   title   class   res   rar   时间   中间   

原文地址:http://www.cnblogs.com/Yilin-cn/p/7101736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!