A JavaScript program to capitalize the first letter of each word in a given string – I don't understand this line of code

huangapple go评论67阅读模式
英文:

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(&quot; &quot;);
  for (let i = 0; i &lt; words.length; i++) {
    const letters = words[i].split(&quot;&quot;);
    letters[0] = letters[0].toUpperCase();
    words[i] = letters.join(&quot;&quot;);
  }
  return words.join(&quot; &quot;);
}

console.log(capFirstLetters(&quot;i wrote a program that capitalizes the first letter of each word in a string.&quot;));

<!-- 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(&quot; &quot;);
  for (var i = 0, x = str.length; i &lt; x; i++) {
    str[i] = str[i][0].toUpperCase() + str[i].substr(1);
  }
  return str.join(&quot; &quot;);
}
console.log(capital_letter(&quot;i wrote a program that capitalizes the first letter of each word in a string.&quot;));

<!-- 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 =&gt; [...str]
  .map((letter,i) =&gt; (i===0 || &quot;.?! &quot;.includes(str[i-1])) ? letter.toUpperCase() : letter)
  .join(&quot;&quot;);

console.log(capFirstLetters(&quot;i wrote a program that capitalizes the first letter of each word in a string.&quot;));

<!-- 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(&quot; &quot;);

So in a console you could do this:

str=&quot;i am a string&quot;;

Which returns:

&#39;i am a string&#39;

and:

str = str.split(&quot; &quot;);

Which returns:

(4)&#160;[&#39;i&#39;, &#39;am&#39;, &#39;a&#39;, &#39;string&#39;]

finally:

str[3];

Which returns:

&#39;string&#39;

huangapple
  • 本文由 发表于 2023年7月10日 15:04:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定