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
Add a ment  | 

3 Answers 3

Reset to default 5

Your 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
Add a ment  | 

3 Answers 3

Reset to default 5

Your 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