Last active
December 25, 2016 03:02
-
-
Save MOOOWOOO/08de8ae46e3d9e5d2f65d9f8003930b5 to your computer and use it in GitHub Desktop.
javascript 字符串操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addBitFront(str, length, bit) { | |
// 字符串指定长度,不足则在前段补位 | |
// str: 待补位的字符串 | |
// length: 指定的长度 | |
// bit: 补位的字符 | |
let new_str = '' | |
if (str.length < length) { | |
new_str = new Array(length - str.length + 1).join(bit) + str // 必须 + 1,否则得到的补位字符串会少 1 位 | |
} | |
return new_str; | |
} | |
function addBitEnd(str, length, bit) { | |
// 后段补位 | |
// str: 待补位的字符串 | |
// length: 指定的长度 | |
// bit: 补位的字符 | |
let bit_len = str.length & length | |
let new_str = '' | |
if (bit_len != 0) { | |
new_set = str + new Array(length - bit_len + 1).join(bit) // 必须 + 1,否则得到的补位字符串会少 1 位 | |
} | |
return new_set | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment