英文:
A JavaScript program to capitalize the first letter of each word in a given string - I don't understand this line of code
问题
我不太明白循环内的这行代码。您可以在不首先从字符串创建数组的情况下,将字符串的字母作为数组的元素来处理吗?
这行代码实际上是将字符串的每个单词拆分成一个数组,然后将该单词的第一个字母大写,然后再将其余部分连接起来。让我解释一下:
str[i] = str[i][0].toUpperCase() + str[i].substr(1);
str[i]
表示当前循环迭代中的单词(字符串)。str[i][0]
获取该单词的第一个字符(字母)。.toUpperCase()
将第一个字符转换为大写字母。str[i].substr(1)
获取该单词除第一个字符外的剩余部分。
然后,这些部分被连接在一起,形成一个首字母大写的单词,并将其赋值给 str[i]
,以替换原始的单词。
所以,答案是,虽然没有显式创建一个字符数组,但通过使用字符串的索引和字符串操作,您可以在循环内处理字符串的字母,从而达到相同的效果。这是 JavaScript 中的一种常见做法。
英文:
I'm new to JS.
I've written a program that capitalizes the first letter of each word in a given string. I also found another piece of code on w3resource.com that does the same, but I don't quite understand how it works.
Here's my code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function capFirstLetters(str) {
const words = str.split(" ");
for (let i = 0; i < words.length; i++) {
const letters = words[i].split("");
letters[0] = letters[0].toUpperCase();
words[i] = letters.join("");
}
return words.join(" ");
}
console.log(capFirstLetters("i wrote a program that capitalizes the first letter of each word in a string."));
<!-- end snippet -->
Here's the code from w3resource.com:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function capital_letter(str) {
str = str.split(" ");
for (var i = 0, x = str.length; i < x; i++) {
str[i] = str[i][0].toUpperCase() + str[i].substr(1);
}
return str.join(" ");
}
console.log(capital_letter("i wrote a program that capitalizes the first letter of each word in a string."));
<!-- end snippet -->
What I don't understand is the line of code inside the for loop. Can you work with the letters of a string as elements of an array without first creating an array from the string?
答案1
得分: 1
以下是您要翻译的内容:
看其他答案以获取分割的解释。
我回答你的另一个问题:是的,您可以将字符串视为字母数组。
但是,由于字符串是不可变的(无法就地更改),我们仍然需要复制它。在这里,我使用了[...spread]来创建数组,但我直接索引字符串。
const capFirstLetters = str => [...str]
.map((letter, i) => (i === 0 || ".?! ".includes(str[i - 1])) ? letter.toUpperCase() : letter)
.join("");
console.log(capFirstLetters("i wrote a program that capitalizes the first letter of each word in a string."));
英文:
See the other answers for the explanation of the split.
I answer your other question: Yes you can access the string as an array of letters.
However since a string is immutable (cannot be changed in place) we need to copy it anyway. Here I use [...spread] to make the array but I index the string directly
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const capFirstLetters = str => [...str]
.map((letter,i) => (i===0 || ".?! ".includes(str[i-1])) ? letter.toUpperCase() : letter)
.join("");
console.log(capFirstLetters("i wrote a program that capitalizes the first letter of each word in a string."));
<!-- end snippet -->
答案2
得分: 0
以下是翻译好的部分:
"split"函数如w3school文档所述,从字符串创建一个数组。以下行创建了一个名为str
的数组变量,从传递给函数的字符串变量str
中创建 - capital_letter(str)
。
str = str.split(" ");
所以在控制台中,你可以这样做:
str = "我是一个字符串";
这将返回:
'我是一个字符串'
以及:
str = str.split(" ");
这将返回:
(4) ['我', '是', '一个', '字符串']
最后:
str[3];
这将返回:
'字符串'
英文:
The split function, as described in w3school documentation, creates an array from a string. The following line creates an array variable called str
, from the string variable called str
, passed into the function - capital_letter(str)
str = str.split(" ");
So in a console you could do this:
str="i am a string";
Which returns:
'i am a string'
and:
str = str.split(" ");
Which returns:
(4) ['i', 'am', 'a', 'string']
finally:
str[3];
Which returns:
'string'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论