C#應用正則表達式完成首字母轉年夜寫的辦法。本站提示廣大學習愛好者:(C#應用正則表達式完成首字母轉年夜寫的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用正則表達式完成首字母轉年夜寫的辦法正文
本文實例講述了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.WordStr(s, @"\b[a-z]\w+", delegate(Match match)
{
string v = match.ToString();
return char.ToUpper(v[0]) + v.Substring(1);
});
}
}
PS:這裡再為年夜家供給2款異常便利的正則表達式對象供年夜家參考應用:
JavaScript正則表達式在線測試對象:
http://tools.jb51.net/regex/javascript
正則表達式在線生成對象:
http://tools.jb51.net/regex/create_reg
願望本文所述對年夜家C#法式設計有所贊助。