设置首字母大写算法(JavaScript)

问题:

返回一个字符串,确保字符串的每一个单词首字母都大写,其他部分小写。html

像'the'和'of'这样的链接符同理。code

解答:

function titleCase(str) {
    var newStr = str.split(" ");
    for(var i = 0; i<newStr.length; i++){
        newStr[i] = newStr[i].slice(0,1).toUpperCase() + newStr[i].slice(1).toLowerCase();
    }
    return newStr.join(" ");
}

titleCase("I'm a little tea pot");

连接:

https://www.w3cschool.cn/code...htm

相关文章
相关标签/搜索