都使用正则表达式
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;
}