字符串分割重组字符串方法
程序员文章站
2022-04-30 18:29:11
...
实现代码
function RegroupStr(group, value, splitstr, maxlen) {
if (typeof value == "string" && value.length > 0) {
var valueLen = value.length;
if (Array.isArray(group) && group.length > 0) {
group = group.sort(function (a, b) {
return a - b;
}).filter((x) => { if (typeof x == "number" && x > 0 && x % 1 === 0) { return x } });
var strsplit = "-";
var newValue = "";
if (splitstr != undefined&& typeof splitstr === "string") {
strsplit = splitstr;
}
if (group.length > 0) {
if (typeof splitstr == "number") {
strsplit = "-";
valueLen = splitstr;
}
if (maxlen && typeof maxlen == "number") {
valueLen = maxlen;
}
if (group.indexOf(valueLen) == -1 && group[group.length - 1] < valueLen) {
group.push(valueLen);
}
group = Array.from(new Set(group));
$.each(group, function (i, _i) {
if (i != 0) { i = group[i - 1]; }
newValue += value.substring(i, _i) + strsplit;
})
while (newValue.substring(newValue.length - 1, newValue.length) == strsplit) {
newValue = newValue.substring(0, newValue.length - 1);
}
return newValue;
}
}
}
return "";
}