如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
当前回答
好吧,所以我是新的JavaScript. 我无法得到上面的为我工作. 所以我开始把它自己。
String name = request.getParameter("name");
name = name.toUpperCase().charAt(0) + name.substring(1);
out.println(name);
在这里,我从一个表格中获取变量(它也手动工作):
String name = "i am a Smartypants...";
name = name.toUpperCase().charAt(0) + name.substring(1);
out.println(name);
出发:“我是聪明的......”;
其他回答
如果您的项目中有Lodash,请使用UpperFirst
var capitalizeMe = "string not starting with capital"
资本化与substr
var capitalized = capitalizeMe.substr(0, 1).toUpperCase() + capitalizeMe.substr(1);
string = string.replace(string.charAt(0), string.charAt(0).toUpperCase());
我更喜欢使用一个以功能方式为导向的解决方案(例如地图序列):
Array.from(str).map((letter, i) => i === 0 ? letter.toUpperCase() : letter ).join('');
咖啡文字
ucfirst = (str) -> str.charAt(0).toUpperCase() + str.slice(1)
作为一个严格的原型方法:
String::capitalize = -> @charAt(0).toUpperCase() + @slice(1)