/** * js截取字符串,中英文都能用 * * @export * @param {string}str * @param {number}len * @returns */ exportfunctioncutStr(str: string, len: number) { let l = 0; let ret = ''; for (let i = 0 , n = str.length; i < n; i++) { let a = str.charAt(i); l++; if (escape(a).length > 4) { //中文字符的长度经编码之后大于4 l++; } ret = ret.concat(a) if (l >= len) { console.log(l) console.log(len) if(i != n - 1) { ret = ret.concat('...') } return ret } } //如果给定字符串小于指定长度,则返回源字符串; if (l < len) { return str } }
guid生成器
可以生成guid
1 2 3 4 5 6
functionguid() { return'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { let r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8) return v.toString(16) }).toUpperCase() }