To capitalize the first letter of a random string, you should follow these steps:

要大写随机字符串的第一个字母,应遵循以下步骤:

  1. Get the first letter of the string;获取字符串的第一个字母;
  2. Convert the first letter to uppercase;将第一个字母转换为大写;
  3. Get the remainder of the string;获取字符串的其余部分;
  4. Concatenate the first letter capitalized with the remainder of the string and return the result;将首个大写字母与字符串的其余部分连接起来并返回结果;

1.获取字符串的第一个字母 (1. Get the First Letter of the String)

You should use the charAt() method, at index 0, to select the first character of the string.

您应该使用索引为0的charAt()方法来选择字符串的第一个字符。

var string = "freeCodecamp";string.charAt(0); // Returns "f"

NOTE: charAt is preferable than using [ ] (bracket notation) as str.charAt(0) returns an empty string ('') for str = '' instead of undefined in case of ''[0].

注意: charAt比使用[ ] (方括号表示法 )更str.charAt(0)因为str.charAt(0) )为str = ''返回空字符串( '' ),而不是在''[0]情况下undefined

2.将首字母转换为大写 (2. Convert the First Letter to uppercase)

You may use toUpperCase() method and convert the calling string to upper case.

您可以使用toUpperCase()方法并将调用字符串转换为大写。

var string = "freeCodecamp";string.charAt(0).toUpperCase(); // Returns "F"

3.获取字符串的余数 (3. Get the Remainder of the String)

You may use slice() method and get the remainder of the string (from the second character, index 1, to the end of the string).

您可以使用slice()方法获取字符串的其余部分(从第二个字符index 1到字符串的末尾)。

var string = "freeCodecamp";string.slice(1); // Returns "reeCodecamp"

4.返回结果加上字符串的第一个字母和其余部分 (4. Return the result adding the first letter and the remainder of the string)

You should create a function that accepts a string as only argument and returns the concatenation of the first letter capitalized string.charAt(0).toUpperCase() and the remainder of the string string.slice(1).

您应该创建一个仅接受字符串作为参数的函数,并返回首字母大写字符串string.charAt(0).toUpperCase()与字符串string.slice(1)其余部分的串联。

var string = "freeCodecamp";function capitalizeFirstLetter(str) {return str.charAt(0).toUpperCase() + str.slice(1);
}capitalizeFirstLetter(string); // Returns "FreeCodecamp"

Or you may add that function to the String.prototype for using it directly on a string using the following code (so that the method is not enumerable but can be overwritten or deleted later):

或者,您可以使用以下代码将该函数添加到String.prototype以便直接在字符串上使用它( 这样该方法不可枚举,但以后可以覆盖或删除 ):

var string = "freeCodecamp";/* this is how methods are defined in prototype of any built-in Object */
Object.defineProperty(String.prototype, 'capitalizeFirstLetter', {value: function () {return this.charAt(0).toUpperCase() + this.slice(1);},writable: true, // so that one can overwrite it laterconfigurable: true // so that it can be deleted later
});string.capitalizeFirstLetter(); // Returns "FreeCodecamp"

资源 (Source)

stackoverflow - Capitalize the first letter of string in JavaScript

stackoverflow-将JavaScript中字符串的首字母大写

翻译自: https://www.freecodecamp.org/news/how-to-capitalize-the-first-letter-of-a-string-in-javascript/

如何在JavaScript中大写字符串的首字母相关推荐

  1. 如何在JavaScript中获取字符串数组的字符串?

    本文翻译自:How do you get a string to a character array in JavaScript? How do you get a string to a chara ...

  2. 如何在JavaScript中反转字符串?

    在不使用内置函数( .reverse() .charAt()等)的情况下,如何在将字符串传递给带有return语句的函数时在JavaScript中将字符串原地(或原地)反向? #1楼 以下技术(或类似 ...

  3. 如何在 JavaScript 中的字符串的字符之间添加空格

    在今天的文章中,我们将学习如何轻松地在 JavaScript 中的字符串字符之间包含空格. 1.String split() 和 Split join() 方法 要在字符串的字符之间添加空格,请对字符 ...

  4. Java实现List中某个对象属性中的字符串参数首字母进行排序

    public static void main(String[] args) {//数组 中文首字母排序// Collator 类是用来执行区分语言环境的 String 比较的,这里选择使用CHINA ...

  5. 如何在 JavaScript 中使字符串的第一个字母大写?

    问题描述: 如何使字符串的第一个字母大写,但不更改任何其他字母的大小写? 例如: "这是一个测试"→"这是一个测试" "埃菲尔铁塔"→&qu ...

  6. 如何在JavaScript中删除字符串的第一个字符

    Let's say you have a string, and you want to remove the first character in it. 假设您有一个字符串,并且想要删除其中的第一 ...

  7. 如何在JavaScript中将字符串的首字母大写?

    如何使字符串的第一个字母大写,但不更改其他任何字母的大小写? 例如: "this is a test" -> "This is a test" " ...

  8. regexp 好汉字符串_如何在JavaScript中使用RegExp确认字符串的结尾

    regexp 好汉字符串 by Catherine Vassant (aka Codingk8) 由凯瑟琳·瓦森(Catherine Vassant)(又名Codingk8) 如何在JavaScrip ...

  9. 如何在javascript中使用多个分隔符分割字符串?

    如何在JavaScript中使用多个分隔符拆分字符串? 我正在尝试在逗号和空格上进行拆分,但是AFAIK,JS的拆分功能仅支持一个分隔符. #1楼 对于那些想要在拆分功能中进行更多自定义的人,我编写了 ...

最新文章

  1. 何恺明时隔2年再发一作论文:为视觉大模型开路,“CVPR 2022最佳论文候选预定”...
  2. 成为软件架构师的4个过程
  3. Jenkins pipeline JENKINS_NODE_COOKIE踩坑记录
  4. while循环练习:
  5. python 工资管理软件_基于[Python]的员工管理系统
  6. TP3.2.x判断手机端访问并设置默认访问模块的方法 - ThinkPHP框架
  7. EasyARM-iMX283A的Linux 开发环境构建
  8. Java实训项目9:GUI学生信息管理系统 - 实现步骤 - 创建数据访问接口
  9. ajax jsonjar包,json-lib.jar
  10. ArrayList扩容
  11. 情人节海报模板,甜到牙疼!
  12. mysql的service name_安装MYSQL出错:a windows service with the name MYSQL already...service解决...
  13. 你需要知道的、有用的 Python 功能和特点
  14. 笔试 | 平安银行笔试题
  15. Java基础总结(上)
  16. 固态硬盘使用一段时间后测速变慢的解决方法
  17. scada系统集成_设计 SCADA 应用程序软件
  18. 计算机快速结束进程,结束进程快捷键,详细教您电脑结束进程快捷键怎么操作...
  19. husky v8 lint-stage eslint
  20. 熵权法(客观赋权法)

热门文章

  1. Connection对象 Statement对象 ResultSet对象
  2. 日期控件的用法 winform
  3. 【loj2585】【APIO2018】新家
  4. 十一、python生成器和迭代器
  5. Firefox 用户加载的半数网页启用了 HTTPS
  6. SQL Server 之 在与SQLServer建立连接时出现与网络相关的或特定于实例的错误
  7. 【知识】OpenStack计算设施----Nova
  8. 苦B的程序猿道路数据验证
  9. 通过sharepoint 客户脚本,验证列表添加页面上的时间
  10. 什么是.Net的异步机制(异步Stream读/写) - step 4