简体   繁体   English

为什么字符串数组中的第一个字母不转换为大写?

[英]Why first letter in array of strings don't convert to upper case?

I take on input string "ZpglnRxqenU" 我接受输入字符串"ZpglnRxqenU"

and must return string with that view "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu" 并且必须返回带有该视图的字符串"Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu"

i almost done, but i don't know why first letter in array elements don't change to upper case. 我几乎完成了,但是我不知道为什么数组元素中的第一个字母不会变为大写。

 function accum(s) { let str = s.toLowerCase(); let arr = []; for(let i=0; i<str.length;i++){ arr.push(str[i].repeat(i+1)); arr[i][0].toUpperCase(); } return arr.join('-'); } console.log(accum("ZpglnRxqenU")); // must be "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu" 

Strings are immutable and just using .toUpperCase() will not update existing string, to do that you can try below code 字符串是不可变的,仅使用.toUpperCase()不会更新现有字符串,为此,您可以尝试以下代码

function accum(s) {
  let str = s.toLowerCase();
  let arr = [];

  for(let i=0; i<str.length;i++){
      arr.push(str[i].repeat(i+1));
      var [firstLetter, ...rest] = arr[i]
      arr[i] = firstLetter.toUpperCase() +rest.join('')
  }

  return arr.join('-');
}

Strings in Javascript are immutable , so it's got to be arr[i] = arr[i][0].toUpperCase() + arr[i].slice(1) if you want to convert it. Javascript中的字符串是不可变的 ,因此如果要进行转换,则必须为arr[i] = arr[i][0].toUpperCase() + arr[i].slice(1)

toUpperCase() only returns the character(s), it won't manipulate the String it was called on. toUpperCase()返回字符, 它不会操纵被调用的String

The same is true for any other method on String.prototype like eg String.replace() . 对于String.prototype上的任何其他方法,例如String.replace()

To make it even more clear: The only way in Javascript to ever change a variable containing a String is assigning a new value to the variable. 更清楚地说:Javascript中要更改包含String的变量的唯一方法是为该变量分配一个新值

 function accum(s) { let str = s.toLowerCase(); let arr = []; for(let i=0; i<str.length; i++){ arr.push(str[i].repeat(i+1)); arr[i] = arr[i][0].toUpperCase() + arr[i].slice(1); } return arr.join('-'); } console.log(accum("ZpglnRxqenU")); 

You can to do it like this way and remove unnecessary this line arr[i][0].toUpperCase(); 您可以像这样进行操作,并删除不必要的这一行arr[i][0].toUpperCase();

 function accum(s) { let str = s.toLowerCase(); let arr = []; for(let i=0; i<str.length;i++){ let gStr = str[i].repeat(i+1); arr.push(gStr[0].toUpperCase() + gStr.slice(1)); } return arr.join('-'); } console.log(accum("ZpglnRxqenU")); 

String are inmutable. 字符串是不可变的。 Using toUpperCase() will not change the original string. 使用toUpperCase()不会更改原始字符串。 Instead of this, it will return you a new string. 取而代之的是,它将返回一个新字符串。 You have to save this new string in the position that you want: 您必须将此新字符串保存在所需的位置:

arr[i][0] = arr[i][0].toUpperCase();

You could take the power of Array.from and get the string in characters and map the first upper case character and the repeated characters for a new string. 您可以利用Array.from并以字符为Array.from获取字符串,然后将第一个大写字符和重复的字符映射为新字符串。

At the end join all items to a single string. 最后,将所有项目连接到单个字符串。

 function accum(s) { return Array .from(s.toLowerCase(), (c, i) => c.toUpperCase() + c.repeat(i)) .join('-'); } console.log(accum("ZpglnRxqenU")); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM