时间:2021-07-01 10:21:17 帮助过:26人阅读
字符串去重
function uniqStr(str) { var str = str.split(''); var zarr = []; for (var i = 0; i < str.length; i++) { if (zarr.indexOf(str[i]) === -1) { zarr.push(str[i]) } } return str.join('') }
将字符放在对象中遍历拼接出字符串
function sc(str) { var obj = { r: "s", o: "i", t: "c", n: "u", F: "a" }; var g = Object.keys(obj).concat(Object.values(obj)); var str = str.split('/'); var sr = ''; for (var i = 0; i < str.length; i++) { sr = sr + g[str[i]]; } return sr; } sc("4/8/3/7/2/6/1/3");//"constructor" sc("7/1/3/5/2/0/8/7/2/1/0")//"Function"
把字符串的奇数位和偶数位分别提取到两个数组中
var str = "abcdef"; var a = str.split(''); var b = []; var c = []; for (var i = 0; i < a.length; i++) { if (i % 2 === 0) { b.push(a[i]); } else { c.push(a[i]); } }
将两个数组按奇数位和偶数位插入拼接为字符串
function jiou(b, c) { var b = b.split(''); var c = c.split(''); var m = []; for (var i = 0; i < b.length; i++) { m.push(1 + i * 2); } for (var j = 0; j < c.length; j++) { b.splice(m[j], 0, c[j]) } return b.join(''); };
好了这篇文章就介绍到这了,感谢js猫同学的分享。