您的位置:首页 > Web前端 > JavaScript

英文字符串第一个字符大写C#和Javascript两种实现方法

2012-06-27 16:22 811 查看
都使用正则表达式

C#

private string ReplaceFirstUper(string eName)
{
Regex rx = new Regex(@"\b(\w)|\s(\w)/g");
string result = rx.Replace(eName.ToLower(), new MatchEvaluator(CapText));
return result;
}

static string CapText(Match m)
{
// Get the matched string.
string x = m.ToString();
// If the first char is lower case...
if (char.IsLower(x[0]))
{
// Capitalize it.
return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
}
return x;
}


Javascript

function ReplaceFirstUper(str)
{
str = str.toLowerCase();
str = str.replace(/\b(\w)|\s(\w)/g, function(m){
return m.toUpperCase();
});
return str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐