slu*_*rrr 50 javascript string capitalize charat
这个功能有什么问题?我失去了感谢你的帮助.
function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
if (splitStr.length[i] < splitStr.length) {
splitStr[i].charAt(0).toUpperCase();
}
str = splitStr.join(' ');
}
return str;
}
titleCase("I'm a little tea pot");
Run Code Online (Sandbox Code Playgroud)
som*_*ere 93
您没有再次将更改分配给阵列,因此您的所有努力都是徒劳的.试试这个:
function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}
document.write(titleCase("I'm a little tea pot"));Run Code Online (Sandbox Code Playgroud)
nim*_*sam 56
我认为这种方式应该更快;因为它不会拆分字符串并再次加入它;只是使用正则表达式。
var str = text.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase());
Run Code Online (Sandbox Code Playgroud)
说明:
(^\w{1}): 匹配字符串的第一个字符|: 或者 (\s{1}\w{1}): 匹配一个空格后的字符g: 全部匹配Mar*_*ude 46
你正在使复杂变得非常容易.您可以在CSS中添加:
.capitalize {
text-transform: capitalize;
}
Run Code Online (Sandbox Code Playgroud)
在javascript中,您可以将类添加到元素中
document.getElementById("element").className="capitalize";
Run Code Online (Sandbox Code Playgroud)
chi*_*ens 32
最短的一个班轮(也非常快):
text.replace(/(^\w|\s\w)/g, m => m.toUpperCase());
Run Code Online (Sandbox Code Playgroud)
解释:
^\w : 字符串的第一个字符| : 或者\s\w : 空格后的第一个字符(^\w|\s\w) 捕捉模式。g 标志:匹配所有出现。如果您想确保其余部分为小写:
text.replace(/(^\w|\s\w)(\S*)/g, (_,m1,m2) => m1.toUpperCase()+m2.toLowerCase())
Run Code Online (Sandbox Code Playgroud)
Ste*_*ush 30
ES6版本:
const toTitleCase = (phrase) => {
return phrase
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
};
let result = toTitleCase('maRy hAd a lIttLe LaMb');
console.log(result);Run Code Online (Sandbox Code Playgroud)
waq*_*qas 18
如果你可以使用thirdparty库,那么lodash有一个帮助函数.
https://lodash.com/docs/4.17.3#startCase
_.startCase('foo bar');
// => 'Foo Bar'
_.startCase('--foo-bar--');
// => 'Foo Bar'
_.startCase('fooBar');
// => 'Foo Bar'
_.startCase('__FOO_BAR__');
// => 'FOO BAR'Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/lodash/4.17.3/lodash.min.js"></script>Run Code Online (Sandbox Code Playgroud)
ans*_*ngh 12
在ES6中使用箭头功能回答一线
const captialize = words => words.split(' ').map( w => w.substring(0,1).toUpperCase()+ w.substring(1)).join(' ')
Run Code Online (Sandbox Code Playgroud)
小智 6
也是一个不错的选择(特别是如果您使用的是freeCodeCamp):
function titleCase(str) {
var wordsArray = str.toLowerCase().split(/\s+/);
var upperCased = wordsArray.map(function(word) {
return word.charAt(0).toUpperCase() + word.substr(1);
});
return upperCased.join(" ");
}
Run Code Online (Sandbox Code Playgroud)
您可以简单地使用正则表达式函数来更改每个字母的大写。通过V8 JIST优化,这应该被证明是快速且高效的内存。
// Only works on Latin-I strings
'tHe VeRy LOOong StRINg'.replace(/\b[a-z]|['_][a-z]|\B[A-Z]/g, function(x){return x[0]==="'"||x[0]==="_"?x:String.fromCharCode(x.charCodeAt(0)^32)})
Run Code Online (Sandbox Code Playgroud)
或者,作为一个功能:
// Only works for Latin-I strings
var fromCharCode = String.fromCharCode;
var firstLetterOfWordRegExp = /\b[a-z]|['_][a-z]|\B[A-Z]/g;
function toLatin1UpperCase(x){ // avoid frequent anonymous inline functions
var charCode = x.charCodeAt(0);
return charCode===39 ? x : fromCharCode(charCode^32);
}
function titleCase(string){
return string.replace(firstLetterOfWordRegExp, toLatin1UpperCase);
}
Run Code Online (Sandbox Code Playgroud)
根据此基准测试,该代码比Chrome中次佳的解决方案快33%以上。
// Only works on Latin-I strings
'tHe VeRy LOOong StRINg'.replace(/\b[a-z]|['_][a-z]|\B[A-Z]/g, function(x){return x[0]==="'"||x[0]==="_"?x:String.fromCharCode(x.charCodeAt(0)^32)})
Run Code Online (Sandbox Code Playgroud)
此例程将处理带连字符的单词和带撇号的单词。
function titleCase(txt) {
var firstLtr = 0;
for (var i = 0;i < text.length;i++) {
if (i == 0 &&/[a-zA-Z]/.test(text.charAt(i)))
firstLtr = 2;
if (firstLtr == 0 &&/[a-zA-Z]/.test(text.charAt(i)))
firstLtr = 2;
if (firstLtr == 1 &&/[^a-zA-Z]/.test(text.charAt(i))){
if (text.charAt(i) == "'") {
if (i + 2 == text.length &&/[a-zA-Z]/.test(text.charAt(i + 1)))
firstLtr = 3;
else if (i + 2 < text.length &&/[^a-zA-Z]/.test(text.charAt(i + 2)))
firstLtr = 3;
}
if (firstLtr == 3)
firstLtr = 1;
else
firstLtr = 0;
}
if (firstLtr == 2) {
firstLtr = 1;
text = text.substr(0, i) + text.charAt(i).toUpperCase() + text.substr(i + 1);
}
else {
text = text.substr(0, i) + text.charAt(i).toLowerCase() + text.substr(i + 1);
}
}
}
titleCase("pAt o'Neil's");
// returns "Pat O'Neil's";
Run Code Online (Sandbox Code Playgroud)
ES2015版本:
const titleCase = title => title
.split(/ /g).map(word =>
`${word.substring(0,1).toUpperCase()}${word.substring(1)}`)
.join("");
Run Code Online (Sandbox Code Playgroud)
小智 5
由于可读性,我通常不喜欢使用正则表达式,而且我也尝试远离循环。我认为这是一种可读性。
function capitalizeFirstLetter(string) {
return string && string.charAt(0).toUpperCase() + string.substring(1);
};
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用现代 JS 语法,这可以让您的生活更轻松。这是我针对给定问题的代码片段:
const capitalizeString = string => string.split(' ').map(item => item.replace(item.charAt(0), item.charAt(0).toUpperCase())).join(' ');
capitalizeString('Hi! i am aditya shrivastwa')Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
95654 次 |
| 最近记录: |