JavaScript首字母大写字符串

huangapple go评论70阅读模式
英文:

JavaScript Capitalizing First Letter of a String

问题

我已经将代码写在HTML文件中,但是在键入lower或console.log(lower)时没有显示任何内容。

然而,当我直接在JS控制台中输入相同的代码

const lower = 'this is a lowercase string';
const upper = lower.charAt(0).toUpperCase() + lower.substring(1);

它完全正常运行。

英文:

I have written the code within the HTML file and it doesn't work typing lower or console.log(lower) does not display anything.

However when I type the same code

const lower = 'this is a lowercase string'
const upper = lower.charAt(0).toUpperCase() + lower.substring(1);

directly into the JS console it works perfectly fine.

答案1

得分: 1

这是一个帮助您的脚本。

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

使用这个原型,您可以将第一个字母大写。

英文:

Here is script to help you.

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

With this prototype, you can make first letter as Uppercase.

huangapple
  • 本文由 发表于 2020年1月6日 20:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定