c# 正则表达式 首字母转大写

时间:2023-03-09 18:54:37
c#  正则表达式 首字母转大写
     class Program
{
static void Main(string[] args)
{
// Input strings.
const string s1 = "samuel allen";
const string s2 = "dot net perls";
const string s3 = "Uppercase first letters of all words in the string."; // Write output strings.
Console.WriteLine(TextTools.UpperFirst(s1));
Console.WriteLine(TextTools.UpperFirst(s2));
Console.WriteLine(TextTools.UpperFirst(s3));
Console.ReadKey();
}
} public static class TextTools
{
/// <summary>
/// Uppercase first letters of all words in the string.
/// </summary>
public static string UpperFirst(string s)
{
return Regex.Replace(s, @"\b[a-z]\w+", delegate(Match match)
{
string v = match.ToString();
return char.ToUpper(v[]) + v.Substring();
});
}
}