我试图写一个函数,大写字符串中每个单词的第一个字母(将字符串转换为标题情况)。

例如,当输入是“我是一个小茶壶”时,我期望“我是一个小茶壶”是输出。然而,该函数返回“i'm a little tea pot”。

这是我的代码:

函数标题案例(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(“ ”); } 返回 str; } console.log(titleCase(“I'm a Little Teapot”));


当前回答

这个例程将处理连字符的单词和带有撇号的单词。

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";

其他回答

ECMA2017或ES8

const titleCase = (string) => { return string .split(' ') .map(word => word.substr(0,1).toUpperCase() + word.substr(1,word.length)) .join(' '); }; let result = titleCase('test test test'); console.log(result); Explanation: 1. First, we pass the string "test test test" to our function "titleCase". 2. We split a string on the space basis so the result of first function "split" will be ["test","test","test"] 3. As we got an array, we used map function for manipulation each word in the array. We capitalize the first character and add remaining character to it. 4. In the last, we join the array using space as we split the string by sapce.

ECMAScript 6版本:

title
    .split(/ /g).map(word =>
        `${word.substring(0,1).toUpperCase()}${word.substring(1)}`)
    .join(" ");
text-transform: capitalize;

CSS已经得到了它:)

我认为这条路会快一些;因为它不会拆分字符串并重新连接;只是用正则表达式。

var str = text.toLowerCase().replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase());

解释:

(^\w{1}):匹配字符串的第一个字符 |:或者 (\s{1}\w{1}):匹配一个空格后面的一个字符 G:全部匹配 match => match. touppercase (): replace with can take function, so;将匹配替换为大写匹配

function titleCase(str) { //First of all, lets make all the characters lower case let lowerCaseString = ""; for (let i = 0; i < str.length; i++) { lowerCaseString = lowerCaseString + str[i].toLowerCase(); } //Now lets make the first character in the string and the character after the empty character upper case and leave therest as it is let i = 0; let upperCaseString = ""; while (i < lowerCaseString.length) { if (i == 0) { upperCaseString = upperCaseString + lowerCaseString[i].toUpperCase(); } else if (lowerCaseString[i - 1] == " ") { upperCaseString = upperCaseString + lowerCaseString[i].toUpperCase(); } else { upperCaseString = upperCaseString + lowerCaseString[i]; } i = i + 1; } console.log(upperCaseString); return upperCaseString; } titleCase("hello woRLD");