如何将字符串的第一个字母设为大写,但不更改任何其他字母的大小写?
例如:
"this is a test"
- >"This is a test"
"the Eiffel Tower"
- >"The Eiffel Tower"
"/index.html"
- >"/index.html"
如何将字符串的第一个字母设为大写,但不更改任何其他字母的大小写?
例如:
"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();
以下是最佳解决方案:
第一解决方案 在CSS中:
p {
text-transform: capitalize;
}
二解决方案 :
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
你也可以把它添加到 String.prototype
所以你可以用其他方法链接它:
String.prototype.capitalizeFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
}
并像这样使用它:
'string'.capitalizeFirstLetter() // String
第三解决方案:
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).toLowerCase();
}
return pieces.join(" ");
}
var string = "hello world";
string = string.charAt(0).toUpperCase() + string.slice(1);
alert(string);
将字符串中所有单词的首字母大写:
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(" ");
}
我们可以得到我最喜欢的第一个角色 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();
});
};
String.prototype.capitalize = function(allWords) {
return (allWords) ? // if all words
this.split(' ').map(word => word.capitalize()).join(' ') : //break down phrase to words then recursive calls until capitalizing all words
this.charAt(0).toUpperCase() + this.slice(1); // if allWords is undefined , capitalize only the first word , mean the first char of the whole string
}
接着:
"capitalize just the first word".capitalize(); ==> "Capitalize just the first word"
"capitalize all words".capitalize(true); ==> "Capitalize All Words"
const capitalize = (string = '') => [...string].map( //convert to array with each item is a char of string by using spread operator (...)
(char, index) => index ? char : char.toUpperCase() // index true means not equal 0 , so (!index) is the first char which is capitalized by `toUpperCase()` method
).join('') //return back to string
然后 capitalize("hello") // Hello