简体   繁体   English

为什么我必须将我的字符串推入数组中才能再次将其重新加入?

[英]Why do I have to push my string into an array only to join it back again?

So I want to capitalize the starting letter of each word in a string. 因此,我想将字符串中每个单词的起始字母大写。 Here's how I did it: 这是我的操作方式:

function LetterCapitalize(str) { 
var arr = str.split(" ");
var newArr = [];
  for(i = 0; i < arr.length; i++) {
    var newStr = arr[i].toString();
    newArr.push(newStr.substring(0,1).toUpperCase() + newStr.substring(1,newStr.length));
  }
  return newArr.join(" ");

}

This code is correct and the first letter of every word in the string was capitalized. 此代码是正确的,并且字符串中每个单词的第一个字母都大写。 However, when I tried to shorten my code like this, it only returned the last word of the string with the first letter capitalized, but nothing else: 但是,当我尝试像这样缩短代码时,它仅返回字符串的最后一个单词,首字母大写,但没有其他内容:

function LetterCapitalize(str) { 
var arr = str.split(" ");
var newArr = [];
  for(i = 0; i < arr.length; i++) {
    var newStr = arr[i].toString();
  }
  return newStr.substring(0,1).toUpperCase() + newStr.substring(1,newStr.length);

}

Basically, what I did was remove the part where I push the new string into a new array, only to join it back into a string. 基本上,我所做的是删除将新字符串推入新数组的部分,只是将其重新组合为字符串。

Problem is in the for loop part code. 问题出在for循环零件代码中。 Here in every iteration the newstr get the value of arr[i].toString(); 在这里,每次迭代时newstr获得arr[i].toString();的值arr[i].toString(); and the old value ie arr[i-1].toString(); 和旧值,即arr[i-1].toString(); which is in newStr is overwritten. 这在newStr被覆盖。 Hence only the last iteration value is stored in newStr 因此,只有最后一次迭代值存储在newStr

for(i = 0; i < arr.length; i++) {
 var newStr = arr[i].toString();
}

Try this: 尝试这个:

 function LetterCapitalize(str) { var arr = str.split(" "); var newStr = ""; for(i = 0; i < arr.length; i++) { var temp = arr[i].toString(); newStr = newStr + temp.substring(0,1).toUpperCase() + temp.substring(1,temp.length)+" "; } return newStr; } alert(LetterCapitalize("hello i am rohit ")); 

Perhaps you got confused by the return in the MAP functionality: 也许您对MAP功能的返回感到困惑:

 function LetterCapitalize(str) { var arr = str.split(" "), newArr = arr.map( function(word) { return word.substring(0,1).toUpperCase() + word.substring(1); }); return newArr.join(" "); } alert(LetterCapitalize("This is a test")) 

You may achieve this through replace function without splitting. 您可以通过替换功能来实现此目的而无需拆分。

function LetterCapitalize(str) { 
  return str.replace(/(^| )([a-z])/g, function(x,y,z) {
    return y+z.toUpperCase()
  }
    )
}
console.log(LetterCapitalize('foo bar buz'))

暂无
暂无

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

相关问题 为什么即使我已将数字转换回字符串,我也不能将数字推送到我的字符串数组中? - Why can I not push a number into my array of strings even though I have converted the number back to a string? 我有一个可切换网站主题的按钮,如果再次单击以更改回默认主题,该如何做相同的按钮? - I have a button that switches the theme of my website how do i make the same button if clicked again to change back to the default Theme? 为什么突然间,我必须推动 GIT 才能看到我的 CSS 更改? - Why, all of a sudden, do I have to push GIT to see my CSS changes? 为什么我必须按两次返回才能回到Backbone? - Why do I have to press back twice to go back in Backbone? 如果我只有字符串形式的 ID,如何从数组中删除 objectId? - How do I delete an objectId from an array if I only have the ID as a string? 为什么我的数组没有按照将对象推入数组的方式排序? - Why is my array not ordering in the manner in which I push the objects into the Array? 不允许将空或未定义的字符串分别推入我的数组 - Do not allow to push empty or undefined string respectively into my array 为什么不能将这些其他数据推入阵列? - Why can't I push this additional data into my array? 为什么我必须在从我的数组中提取之前从我的参数中提取以从数组中删除一个项目? - Why do I have to pull from my argument before pulling from my array wrt to removing an item from an array? 为什么我只能返回 null 值? - Why do I only get null values ​back?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM