Using javascript how to write a function to add space at end of string

Bharat Guru
Bharat Guru used Ask the Experts™
on
How can I add blank space after value with max length should be 7 character.

var strTest = 'TEST'
strTest = formatstring(strTest, 7, ' ')

strTest result should be  'TEST   ';

function formatstring(strString, maxlendth, repalcewith){
......
}

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
strTest = strTest  + '  ';
Commented:

var test = 'Name';
for(var i = test.length; i < 7; i++){
test = test + ' ';
}
alert(test.length);

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
function formatstring(strString, maxlendth, repalcewith){
    return (strString + "       ".split(" ").join(repalcewith)).substr(0,maxlendth)
}

Open in new window

Author

Commented:
Thank

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial