C#使用正则表达式实现首字母转大写的方法
本文实例讲述了C#使用正则表达式实现首字母转大写的方法。分享给大家供大家参考,具体如下:
classProgram
{
staticvoidMain(string[]args)
{
//Inputstrings.
conststrings1="samuelallen";
conststrings2="dotnetperls";
conststrings3="Uppercasefirstlettersofallwordsinthestring.";
//Writeoutputstrings.
Console.WriteLine(TextTools.UpperFirst(s1));
Console.WriteLine(TextTools.UpperFirst(s2));
Console.WriteLine(TextTools.UpperFirst(s3));
Console.ReadKey();
}
}
publicstaticclassTextTools
{
///<summary>
///Uppercasefirstlettersofallwordsinthestring.
///</summary>
publicstaticstringUpperFirst(strings)
{
returnRegex.Replace(s,@"\b[a-z]\w+",delegate(Matchmatch)
{
stringv=match.ToString();
returnchar.ToUpper(v[0])+v.Substring(1);
});
}
}
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述对大家C#程序设计有所帮助。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短