Skip to content

Instantly share code, notes, and snippets.

@daanta-real
Created October 28, 2022 06:09
Show Gist options
  • Select an option

  • Save daanta-real/1ea2a5db4fea4becf71875fbf9c12c91 to your computer and use it in GitHub Desktop.

Select an option

Save daanta-real/1ea2a5db4fea4becf71875fbf9c12c91 to your computer and use it in GitHub Desktop.
Get Padded String
// String 채우기(padding)
// 사용법: getPaddedStr(원본문자열, 패딩이 포함된 총 길이, 채울 문자열, 채울 방향)
// ex) getPaddedStr("1135j", 10, "#", "left") = "#####1135j"
function getPaddedStr(strOrg, width, to, direction) {
if(!strOrg) return "";
var strOrg = new String(strOrg), len = strOrg.length;
if(len >= width) return strOrg;
var strPad = new Array(width - len + 1).join(to);
return (direction == "left"
? strPad + strOrg
: strOrg + strPad
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment