简体   繁体   English

将字符串中的每个第一个字符转换为大写

[英]Transformation of each first character in a string to uppercase

I found a solution to the code that makes every first letter of a string to uppercase.我找到了使字符串的每个第一个字母都大写的代码的解决方案。 But for me it's not the simplest version to understand.但对我来说,这不是最容易理解的版本。 Is there perhaps more easier way to rewrite it?也许有更简单的方法来重写它?

Let me try to explain the code for you.让我尝试为您解释代码。

function titleCase(str) {
   var splitStr = str.toLowerCase().split(' ');
   for (var i = 0; i < splitStr.length; i++) {
       splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);     
   }
   return splitStr.join(' '); 
}

document.write(titleCase("I'm a little tea pot"));

Let's say we have the sentence "javascript is cool" and we want to capitalize that.假设我们有一句话“javascript is cool”,我们想把它大写。

So we start out by declaring the variable splitStr.所以我们首先声明变量 splitStr。 This is an array of every word in the sentence.这是句子中每个单词的数组。 This array is obtained by "splitting" the string by the spaces.该数组是通过空格“拆分”字符串获得的。 As a result, in our case, splitStr is ["javascript", "is", "cool"].因此,在我们的例子中,splitStr 是 ["javascript", "is", "cool"]。

Now, we go into this for loop that loops through every element in splitStr.现在,我们进入这个 for 循环,它遍历 splitStr 中的每个元素。 For every element in splitStr, the loop replaces that element with a word formed by concatenating the capitalized first letter of the corresponding word in the array, followed by the rest of the word.对于 splitStr 中的每个元素,循环用一个单词替换该元素,该单词通过连接数组中相应单词的大写首字母,然后是单词的其余部分而形成。 For example:例如:

javascript = J + avascript = Javascript javascript = J + avascript = Javascript

This happens for every word in the array.数组中的每个单词都会发生这种情况。 In the end, the array now contains: ["Javascript", "Is", "Cool"].最后,数组现在包含:["Javascript", "Is", "Cool"]。

At the every end, we join the array together separating each element with a space which results in the string "Javascript Is Cool".在每一端,我们将数组连接在一起,用空格分隔每个元素,从而产生字符串“Javascript Is Cool”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式匹配字符串中的第一个大写字符 - Regex match from first uppercase character in string 在JavaScript中将字符串中每个单词的首字母转换为大写 - Convert first letter of each word in a string to Uppercase in JavaScript 正则表达式检查第一个字符是否为大写 - Regex to check if the first character is uppercase Javascript 删除字符串每行的第一个字符 - Javascript Delete First Character on Each Line of String 返回字符串中每个单词的第一个字符 - Return First Character of each word in a string 给定一个字符串数组,将每个字符串转换为: 如果第一个字母大写则大写 如果第一个字母小写则小写 - Given an array of strings, convert each string into: uppercase if first letter is capital lower case if first letter is small 在 JavaScript 中将字符串的第一个元素大写 - upperCase the first element of a string in JavaScript 正则表达式模式,用于检查字符串中每个单词的第一个字母,如果它是Javascript中的大写字母 - Regex Pattern for checking the first letter of each word in a string if its Uppercase in Javascript 如何用特定的indexOf将字符替换为字符串中的大写字母? - How to replace a character with an specific indexOf to uppercase in a string? Javascript 检查第一个字符是数字、大写、小写还是特殊字符 - Javascript check if first character is number, upperCase, lowerCase or special character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM