【问题标题】:How can I capitalize a string? [duplicate]如何将字符串大写? [复制]
【发布时间】:2021-05-06 23:57:32
【问题描述】:

我有这个使用 JavaScript 的字符串:

let str = "my city is beautiful."

我想得到:

"My City Is Beautiful"

我试过这个:

str.charAt(0).toUpperCase() + str.slice(1)

但我得到了:

My city is beautiful

你能帮帮我吗?

【问题讨论】:

标签: javascript


【解决方案1】:

您可以使用split、map 和join:

"my city is beautiful"
  .split(" ")
  .map(s => s.charAt(0).toUpperCase() + s.slice(1))
  .join(" ")

【讨论】:

    【解决方案2】:
    const string = "my city is beautiful.";
    const words = string.split(" ");
    
    for (let i = 0; i < words.length; i++) {
        words[i] = words[i][0].toUpperCase() + words[i].substr(1);
    }
    
    let newString = words.join(" ");
    

    【讨论】:

      【解决方案3】:

      使用map 和join。

      let str = "my city is beautiful."
      
      var newStr = str.split(" ").map( str =>  str.substring(0,1).toUpperCase()+ str.substring(1)).join(' ')
      
      console.log(newStr)

      注意这里的逻辑是:

      • 拆分每个单词
      • 使用map 进行迭代,并且对于每个单词:将第一个值设置为大写,并使用substring(1) 附加单词的其余部分。
      • 并加入所有值以再次获取该短语。

      【讨论】:

        【解决方案4】:

        您需要做的就是循环遍历字符串中的每个单词并单独修改它。 您可以使用 split() 来做到这一点。

        let str = "my city is beautiful."
        let newStr = "" // Create temporary string
        
        let words = str.split(" ") // Split the string into words and store them in array
        for (word of words) { // Cycle through the array
            let newWord = word.charAt(0).toUpperCase() + word.slice(1) // Modify every word in the array
            newStr += (newWord + " ") // Append the word to the new String and add a space to the 
        end
        }
        
        console.log(newStr.trim()) // Removes the spaces at the beginning and the end
        // Output:
        // "My City Is Beautiful."
        

        或者更短的方法:

        let str = "my city is beautiful."
        let newStr = ""
        
        for (word of str.split(" "))
          newStr += (word.charAt(0).toUpperCase() + word.slice(1) + " ")
        
        console.log(newStr.trim())
        // Output:
        // "My City Is Beautiful."
        

        【讨论】:

          【解决方案5】:

          你可以:

          1. 将字符串拆分为单词
          2. 将每个单词的首字母大写
          3. 将单词重新组合成一个字符串

          let str = "my city is beautiful."
          const result =
          
            // split up the string into words
            str.split(' ')
            
            // capitalize the first letter of each word
            .map(word => {
              const [firstChar, ...rest] = word;
              return [firstChar.toUpperCase(), ...rest].join('');
            })
            
            // join the words back into a single string
            .join(' ');
            
          console.log(result);

          【讨论】:

            【解决方案6】:

            在此处了解如何操作:

            Basic Algorithm Scripting: Title Case a SentencePassed

            freeCodeCamp Challenge Guide: Title Case a Sentence

            let str = "my city is beautiful."
            
            let newstr = str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase());
            console.log(newstr)

            【讨论】:

              猜你喜欢
              • 2014-12-21
              • 2012-07-08
              • 1970-01-01
              • 1970-01-01
              • 2011-03-08
              • 2010-11-12
              • 2014-11-26
              • 2011-08-18
              相关资源
              最近更新 更多