如何使字符串的第一个字母大写,但不更改其他任何字母的大小写?
例如:
"this is a test"
-> "This is a test"
"the Eiffel Tower"
-> "The Eiffel Tower"
"/index.html"
-> "/index.html"
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
其他一些答案修改了String.prototype
(这个答案也曾经使用过),但是由于可维护性,我现在不建议这样做(很难找出将函数添加到prototype
,如果其他代码使用相同的代码,可能会导致冲突。名称 / 浏览器会在将来添加具有相同名称的本机功能)。
这是一种更加面向对象的方法:
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
您可以像下面这样调用该函数:
"hello world".capitalize();
预期输出为:
"Hello world"
在 CSS 中:
p:first-letter {
text-transform:capitalize;
}
这是流行答案的简化版本,它通过将字符串视为数组来获得第一个字母:
function capitalize(s)
{
return s[0].toUpperCase() + s.slice(1);
}
更新:
根据下面的评论,这在 IE 7 或更低版本中不起作用。
更新 2:
为了避免undefined
空字符串(请参见下面的 @ njzk2 的注释 ),您可以检查一个空字符串:
function capitalize(s)
{
return s && s[0].toUpperCase() + s.slice(1);
}
这是基于此 jsperf 测试的最快方法(从最快到最慢的顺序)。
如您所见,前两种方法在性能上基本上是可比的,而更改String.prototype
到目前为止是性能最慢的。
// 10,889,187 operations/sec
function capitalizeFirstLetter(string) {
return string[0].toUpperCase() + string.slice(1);
}
// 10,875,535 operations/sec
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// 4,632,536 operations/sec
function capitalizeFirstLetter(string) {
return string.replace(/^./, string[0].toUpperCase());
}
// 1,977,828 operations/sec
String.prototype.capitalizeFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
在另一种情况下,我需要将其首字母大写,将其余字母小写。以下情况使我更改了此功能:
//es5
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
capitalize("alfredo") // => "Alfredo"
capitalize("Alejandro")// => "Alejandro
capitalize("ALBERTO") // => "Alberto"
capitalize("ArMaNdO") // => "Armando"
// es6 using destructuring
const capitalize = ([first,...rest]) => first.toUpperCase() + rest.join('').toLowerCase();
这是 2018 ECMAScript 6 + 解决方案 :
const str = 'the Eiffel Tower';
const newStr = `${str[0].toUpperCase()}${str.slice(1)}`;
console.log('Original String:', str); // the Eiffel Tower
console.log('New String:', newStr); // The Eiffel Tower
将字符串中所有单词的首字母大写:
function ucFirstAllWords( str )
{
var pieces = str.split(" ");
for ( var i = 0; i < pieces.length; i++ )
{
var j = pieces[i].charAt(0).toUpperCase();
pieces[i] = j + pieces[i].substr(1);
}
return pieces.join(" ");
}
如果您已经(或正在考虑)使用lodash
,则解决方案很简单:
_.upperFirst('fred');
// => 'Fred'
_.upperFirst('FRED');
// => 'FRED'
_.capitalize('fred') //=> 'Fred'
查看他们的文档: https : //lodash.com/docs#capitalize
_.camelCase('Foo Bar'); //=> 'fooBar'
https://lodash.com/docs/4.15.0#camelCase
_.lowerFirst('Fred');
// => 'fred'
_.lowerFirst('FRED');
// => 'fRED'
_.snakeCase('Foo Bar');
// => 'foo_bar'
Vanilla js 的第一个大写字母:
function upperCaseFirst(str){
return str.charAt(0).toUpperCase() + str.substring(1);
}
我们可以使用我最喜欢的RegExp
来获得第一个字符,它看起来像一个可爱的笑脸: /^./
String.prototype.capitalize = function () {
return this.replace(/^./, function (match) {
return match.toUpperCase();
});
};
对于所有咖啡迷:
String::capitalize = ->
@replace /^./, (match) ->
match.toUpperCase()
... 对于所有认为有更好方法的人,而无需扩展本机原型:
var capitalize = function (input) {
return input.replace(/^./, function (match) {
return match.toUpperCase();
});
};