programming-examples/js/String/Pad (left, right) a string to get to a determined length.js

15 lines
469 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
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,''));