[英]How to auto capitalize the first character in a string?
我想自動大寫字符串中的第一個字符,例如,如果我通過鍵盤輸入“ john”,則變量中保存的數據將是“ John”。 那么如何將32添加到字符串的第一個字符以大寫呢? 請給我指路。 謝謝。
您應該自己編寫,例如
Console.WriteLine(CapitalizeFirst("cihan"));
private string CapitalizeFirst(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
return char.ToUpper(s[0]) + s.Substring(1);
}
然后輸出將為Cihan
$("input").keyup(function(event){ if(event.ctrlKey || event.altKey){ return; }else{ $(this).val($(this).val().toUpperCase()); str = $(this).val(); str = str.replace(/\\w\\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); $(this).val(str); } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.