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#程序设计有所帮助。