admin管理员组文章数量:1022777
var code ='';
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
code = code+branch;
}
alert(code);// result: 123 123 123 etc..
var code ='';
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
code = code+branch;
}
alert(code);// result: 123 123 123 etc..
I have branch string var and code var. If I do alert branch+"t" I get 123t, so I suppose I don't have any spaces at the end in my branch var.
But after doing for loop and alerting code var I get 123 123 123, so I get spaces added after each concatenation of branch to code var. What can be the problem?
Share
Improve this question
asked Nov 4, 2015 at 20:22
yerassylyerassyl
3,0576 gold badges45 silver badges69 bronze badges
1
- 1 what is the value of branch variable? – The Reason Commented Nov 4, 2015 at 20:25
3 Answers
Reset to default 5Your main problem may be a space in the left side, not in the right. So,try trimming your data.
var code ='';
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
//the .trim() here will handle the spaces
code = code+branch.trim();
}
alert(code);
Why?
Well, trimming is a well known practice in back-end development because you never can predict exactly what is going into your variables. So, trimming will remove all spaces from both sides of your string. I think this is your way to go, validating your data is always safe.
It seems like branch has an extra space
branch = ' 123'.
Just make sure you remove it and it won't append extra spaces each time.
I belive you didn't copy your entire code. The "branch" var cannot 123, if it was the case, you would not see a space:
https://jsfiddle/59hqc2ck/
var code ='';
var branch = 123;
var endVar = 10;
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
code = code+branch;
}
alert(code);
var code ='';
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
code = code+branch;
}
alert(code);// result: 123 123 123 etc..
var code ='';
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
code = code+branch;
}
alert(code);// result: 123 123 123 etc..
I have branch string var and code var. If I do alert branch+"t" I get 123t, so I suppose I don't have any spaces at the end in my branch var.
But after doing for loop and alerting code var I get 123 123 123, so I get spaces added after each concatenation of branch to code var. What can be the problem?
Share
Improve this question
asked Nov 4, 2015 at 20:22
yerassylyerassyl
3,0576 gold badges45 silver badges69 bronze badges
1
- 1 what is the value of branch variable? – The Reason Commented Nov 4, 2015 at 20:25
3 Answers
Reset to default 5Your main problem may be a space in the left side, not in the right. So,try trimming your data.
var code ='';
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
//the .trim() here will handle the spaces
code = code+branch.trim();
}
alert(code);
Why?
Well, trimming is a well known practice in back-end development because you never can predict exactly what is going into your variables. So, trimming will remove all spaces from both sides of your string. I think this is your way to go, validating your data is always safe.
It seems like branch has an extra space
branch = ' 123'.
Just make sure you remove it and it won't append extra spaces each time.
I belive you didn't copy your entire code. The "branch" var cannot 123, if it was the case, you would not see a space:
https://jsfiddle/59hqc2ck/
var code ='';
var branch = 123;
var endVar = 10;
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
code = code+branch;
}
alert(code);
本文标签: JavaScript string concatenation adds extra space at the endStack Overflow
版权声明:本文标题:JavaScript string concatenation adds extra space at the end - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745507783a2153684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论