Skip to content

Latest commit

 

History

History
32 lines (22 loc) · 1.02 KB

capitalize.md

File metadata and controls

32 lines (22 loc) · 1.02 KB
标题 标签
capitalize(首字母大写) string,intermediate(字符串,两者之间的)

将字符串的第一个字母大写。

  • 使用数组解构和 String.prototype.toUpperCase() 将字符串的第一个字母大写。
  • 使用 Array.prototype.join('') 将大写的第一个与 ...其余字符组合在一起。
  • 省略lowerRest参数以保持字符串的其余部分不变,或将其设置为 true 以转换为小写。

代码如下:

const capitalize = ([first, ...rest], lowerRest = false) =>
  first.toUpperCase() +
  (lowerRest ? rest.join('').toLowerCase() : rest.join(''));

调用方式:

capitalize('fooBar'); // 'FooBar'
capitalize('fooBar', true); // 'Foobar'

应用场景

结果如下:

<iframe src="codes/javascript/html/capitalize.html"></iframe>