string/omitStr.jsjavascript
/**
* 格式化替换字符串
* @function omitStr
* @memberof module:string
* @param { string } str 需要操作的字符串
* @param { number } start 开始位置
* @param { number } end 结束位置 3 的话就是倒数第三个 倒数第三个不被替换
* @param { string } replaceStr 替换的值 默认 '*'
* @example
* omitStr('185098535', 2, 3, '*') // 18****535
*/
function omitStr(str, start, end, replaceStr = '*') {
if (typeof str !== 'string' || !Number.isInteger(start) || !Number.isInteger(end) || typeof replaceStr !== 'string') {
throw new TypeError('str and replaceStr must be strings; start and end must be integers')
}
if (start < 0 || end < 0 || start + end > str.length) throw new RangeError('start and end must identify a range within str')
const startPosition = start
const endPosition = end
const count = str.length - startPosition - endPosition
return str.slice(0, startPosition) + replaceStr.repeat(count) + str.slice(count + startPosition)
}
export default omitStr