You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
programming-examples/js/String/Pad (left, right) a string ...

15 lines
469 B
JavaScript

function formatted_string(pad, user_str, pad_pos)
{
if (typeof user_str === 'undefined')
return pad;
if (pad_pos == 'l')
{
return (pad + user_str).slice(-pad.length);
}
else
{
return (user_str + pad).substring(0, pad.length);
}
}
console.log(formatted_string('0000',123,'l'));
console.log(formatted_string('00000000',123,''));