javascript - How to delete last white space in the array when concatenating? -
i need have javascript alert without space between last word , exclamation mark.
the final alert should like:
i own garden verymuchmuch verymuchmuch!
here code have space before.
var inittext, closetext, msg = ""; var i, j, k; var myarray = new array(3); myarray[0] = "my"; myarray[1] = "own"; myarray[2] = "garden"; inittext = "i "; closetext = "!"; (k=0; k < 3; k++) { inittext = inittext + myarray[k] + " "; } (j=0; j < 2; j++){ inittext = inittext + "very"; (i=0; < 2; i++){ inittext = inittext + "much"; } inittext = inittext +" "; } msg = inittext + closetext; alert(msg);
you check not add white space in way:
var inittext, closetext, msg = ""; var i, j, k; var myarray = new array(3); myarray[0] = "my"; myarray[1] = "own"; myarray[2] = "garden"; inittext = "i "; closetext = "!"; (k=0; k < 3; k++) { inittext = inittext + myarray[k] + " "; } (j=0; j < 2; j++){ inittext = inittext + "very"; (i=0; < 2; i++){ inittext = inittext + "much"; } inittext = inittext + (j<1 ? " " : ""); } msg = inittext + closetext; alert(msg);
or can trim inittex variable msg = inittext.trim() + closetext;
Comments
Post a Comment