javascript - 如何将字符串大写?
问题描述
我有这个使用 JavaScript 的字符串:
let str = "my city is beautiful."
我想得到:
"My City Is Beautiful"
我试过这个:
str.charAt(0).toUpperCase() + str.slice(1)
但我得到了:
My city is beautiful
请问你能帮帮我吗?
解决方案
您可以使用split
,map
和join
:
"my city is beautiful"
.split(" ")
.map(s => s.charAt(0).toUpperCase() + s.slice(1))
.join(" ")
推荐阅读
- python - Python:将数据写入受保护的 .xlsm Excel 文件中的列
- rdf - OWL/SHACL 和 RIF 的区别
- kubernetes - 如何在 Kubernetes 中删除守护程序集和服务
- twitter-bootstrap - bootstrap 4 导航栏固定在顶部,在滑块上透明,如果滑块没有背景,则为背景
- tensorflow - 在 macOS el capitan 的 Tensorflow 0.12.0 版本中运行 CNN 的问题
- git - 在 SourceTree 中签出旧版本后看不到最新的 GIT 提交,没有远程
- c# - Humanizer 无法在 C# 中对意大利语单词进行单数化或复数化
- java - 使用 java.net.URLEncoder 时不要编码欧元符号
- bash - 用于将当前迭代的值与上一次迭代的值进行比较的 Shell 脚本
- c# - 使用 EF Code First 的 AutoMapper 无限循环