您的位置:首页 > 其它

[字符串处理]替换字符串中间位置字符为指定字符

2018-02-24 10:36 393 查看
示例:

$.fl.stringReplace('12345678', {start: 2, end: 2}) # 12****78

$.fl.stringReplace('12345678', {format: '-'}) # 12----78
stringReplace: function(str, opt) {
var endStr, middleStr, middleStrReplace, options, startStr;
options = {
format: (opt != null ? opt.format : void 0) || "*",
start: (opt != null ? opt.start : void 0) || 1,
end: (opt != null ? opt.end : void 0) * -1 || 1 * -1
};
startStr = str.slice(0, options.start);
middleStr = str.slice(options.start, options.end);
endStr = str.slice(options.end);
middleStrReplace = new Array(middleStr.length + 1).join(options.format);
if (str.length > 2) {
return startStr + middleStrReplace + endStr;
} else {
return new Array(str.length + 1).join(options.format);
}
},


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: