【问题标题】:Uppercase first letter of only the first word [duplicate]仅第一个单词的大写首字母[重复]
【发布时间】:2023-04-04 12:33:02
【问题描述】:

可能重复:
Capitalize the first letter of string in JavaScript

如何强制字段中第一个单词的第一个字母为大写?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您不需要 JavaScript,使用 CSS :first-letter 伪元素就足够了。

    如果你想通过 JavaScript 来做,那就是asked before:

    str.replace(/^\w/, function($0) { return $0.toUpperCase(); })
    

    【讨论】:

      【解决方案2】:

      之前问过:How do I make the first letter of a string uppercase in JavaScript?

      正确答案:

      function capitalizeFirstLetter(string)
      {
          return string.charAt(0).toUpperCase() + string.slice(1);
      }
      

      你可以这样使用它:

      var myString = "hello world";
      alert(capitalizeFirstLetter(myString));
      

      它会提醒Hello world

      【讨论】:

      • 第一个单词的第一个字母。如果字符串中的第一个字符是空格,则不起作用。
      • 你说得对。
      猜你喜欢
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      相关资源
      最近更新 更多