标题案例一句话? [英] Title case a sentence?
本文介绍了标题案例一句话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在javascript中正确处理一个字符串 - 到目前为止,我有这样的代码:
这似乎没有大写第一个字母,我也坚持如何小写所有字母在第一个字母之后。
I'm trying to proper case a string in javascript - so far I have this code: This doesn't seem to capitalize the first letter, and I'm also stuck on how to lowercase all the letters after the first letter.
function titleCase(str) {
var newstr = str.split(" ");
for(i=0;i<newstr.length;i++){
newstr[i].charAt(0).toUpperCase();
}
newstr = newstr.join(" ");
return newstr;
}
要说清楚,我希望句子中的每个单词都大写。
To be clear, I want every single word in the sentence to be capitalized.
推荐答案
这应该有效。请注意我如何将 newstr [i]
设置为所需的输出。像 .toUpperCase()
这样的函数不会影响原始字符串。它们只返回带有所需属性的 new 字符串。
This should work. Notice how I set newstr[i]
to the desired output. Functions like .toUpperCase()
do not affect the original string. They only return a new string with the desired property.
function titleCase(str) {
var newstr = str.split(" ");
for(i=0;i<newstr.length;i++){
if(newstr[i] == "") continue;
var copy = newstr[i].substring(1).toLowerCase();
newstr[i] = newstr[i][0].toUpperCase() + copy;
}
newstr = newstr.join(" ");
return newstr;
}
这篇关于标题案例一句话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文