/** * 字符串工具类 * @author 薛鸿潇 */ export default class StringUtil { /** * 颜色表 */ private str_color = { /** 红色1 */ red1: '#be2c1d', /** 棕色1 */ bro1: '#4E301B', } /** * 将数据表里的字符串转成相对颜色 * @param 需要转换的字符串 比如 "你好[red中国]" 其中“中国”为红色...你好[red{s40}中国] * @return 返回一个富文本字符串 * @author xhx */ public getTextByStringInData(str: string): string { var newStr: string = str; var i: number = 0; var begin: number; var end: number; var sub: string; var searchValue: string; var replaceValue: string; var color_key: string; // 解析颜色 while (i < str.length) { begin = str.indexOf("[", i); end = str.indexOf("]", begin); if (begin != -1 && end != -1)//把[redXXX]转成富文本 { searchValue = str.slice(begin, end + 1); // 颜色内容 color_key = str.slice(begin + 1, begin + 5); let color_str = this.str_color[color_key] if (color_str) { sub = str.slice(begin + 6, end); replaceValue = this.getColorStringColor(sub, color_str); newStr = newStr.replace(searchValue, replaceValue); } i = end; } else { break; } } let size_index: number = 0; let size_begin: number = 0; let size_end: number = 0; let size_searchValue: string; // let size_sub: string // 解析尺寸{40=100%},100%是40尺寸.例子2:[bro对目标造成][red{40=104%}][bro攻击伤害] while (size_index < newStr.length) { size_begin = newStr.indexOf("{", size_index); size_end = newStr.indexOf("}", size_begin); if (size_begin != -1 && size_end != -1) { // 尺寸内容 size_searchValue = newStr.slice(size_begin, size_end + 1); let size_value = newStr.slice(size_begin + 1, size_begin + 3); let size_sub = size_searchValue.slice(4, size_searchValue.length - 1); if (size_sub) { let new_value = '' + size_sub + ''; newStr = newStr.replace(size_searchValue, new_value); } size_index = size_end; } else { break; } } return newStr; } /**按品质转换字符串颜色 */ private getColorStringColor(str: string = '#FFFFFF', color_str: string): string { return '' + str + ''; } /** * 阿拉伯数字 转 汉字 * @param num 要转的数字 * @returns 汉字数字 * @author xhx */ public arabicToChinese(num: number): string { let unit_section = ['', '万', '亿', '万亿', '亿亿']; let unit_chn = ['', '十', '百', '千']; let chars = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; let unit_index = 0; let str_ins = '', zh_str = ''; let need_zero = false; let SectionToChinese = function (section) { let _zh_str = ''; let _unit_index = 0; let zero = true; while (section > 0) { let v = section % 10; let _str_ins = ''; if (v === 0) { if (!zero) { zero = true; _zh_str = chars[v] + _zh_str; } } else { zero = false; if (_unit_index !== 1 || section > 1) { _str_ins = chars[v]; } _str_ins += unit_chn[_unit_index]; _zh_str = _str_ins + _zh_str; } ++_unit_index; section = Math.floor(section / 10); } return _zh_str; }; if (num === 0) { return chars[0]; } while (num > 0) { let section = num % 10000; if (need_zero) { zh_str = chars[0] + zh_str; } str_ins = SectionToChinese(section); str_ins += (section !== 0) ? unit_section[unit_index] : unit_section[0]; zh_str = str_ins + zh_str; need_zero = (section < 1000) && (section > 0); num = Math.floor(num / 10000); ++unit_index; } return zh_str; } /** * 限制字符串长度,超出部分用...代替 * @param str 要拆分的字符串 * @param max_len 最大长度 * @returns 被限制后的字符串 * @author xhx */ public getNewLimitStr(str: string, max_len: number) { let arr_str = str.split(""); let new_str = ''; max_len = arr_str.length >= max_len ? max_len : arr_str.length;//字符串不能超出的长度,否则用...显示 for (let i = 0; i < max_len; i++) { if (i == (max_len - 1)) { new_str += '...'; } else { new_str += arr_str[i]; } } return new_str; } }