StringUtil.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * 字符串工具类
  3. * @author 薛鸿潇
  4. */
  5. export default class StringUtil {
  6. /**
  7. * 颜色表
  8. */
  9. private str_color = {
  10. /** 红色1 */
  11. red1: '#be2c1d',
  12. /** 棕色1 */
  13. bro1: '#4E301B',
  14. }
  15. /**
  16. * 将数据表里的字符串转成相对颜色
  17. * @param 需要转换的字符串 比如 "你好[red中国]" 其中“中国”为红色...你好[red{s40}中国]
  18. * @return 返回一个富文本字符串
  19. * @author xhx
  20. */
  21. public getTextByStringInData(str: string): string {
  22. var newStr: string = str;
  23. var i: number = 0;
  24. var begin: number;
  25. var end: number;
  26. var sub: string;
  27. var searchValue: string;
  28. var replaceValue: string;
  29. var color_key: string;
  30. // 解析颜色
  31. while (i < str.length) {
  32. begin = str.indexOf("[", i);
  33. end = str.indexOf("]", begin);
  34. if (begin != -1 && end != -1)//把[redXXX]转成富文本
  35. {
  36. searchValue = str.slice(begin, end + 1);
  37. // 颜色内容
  38. color_key = str.slice(begin + 1, begin + 5);
  39. let color_str = this.str_color[color_key]
  40. if (color_str) {
  41. sub = str.slice(begin + 6, end);
  42. replaceValue = this.getColorStringColor(sub, color_str);
  43. newStr = newStr.replace(searchValue, replaceValue);
  44. }
  45. i = end;
  46. }
  47. else {
  48. break;
  49. }
  50. }
  51. let size_index: number = 0;
  52. let size_begin: number = 0;
  53. let size_end: number = 0;
  54. let size_searchValue: string;
  55. // let size_sub: string
  56. // 解析尺寸{40=100%},100%是40尺寸.例子2:[bro对目标造成][red{40=104%}][bro攻击伤害]
  57. while (size_index < newStr.length) {
  58. size_begin = newStr.indexOf("{", size_index);
  59. size_end = newStr.indexOf("}", size_begin);
  60. if (size_begin != -1 && size_end != -1) {
  61. // 尺寸内容
  62. size_searchValue = newStr.slice(size_begin, size_end + 1);
  63. let size_value = newStr.slice(size_begin + 1, size_begin + 3);
  64. let size_sub = size_searchValue.slice(4, size_searchValue.length - 1);
  65. if (size_sub) {
  66. let new_value = '<size = ' + size_value + '>' + size_sub + '</color>';
  67. newStr = newStr.replace(size_searchValue, new_value);
  68. }
  69. size_index = size_end;
  70. } else {
  71. break;
  72. }
  73. }
  74. return newStr;
  75. }
  76. /**按品质转换字符串颜色 */
  77. private getColorStringColor(str: string = '#FFFFFF', color_str: string): string {
  78. return '<color=' + color_str + '>' + str + '</color>';
  79. }
  80. /**
  81. * 阿拉伯数字 转 汉字
  82. * @param num 要转的数字
  83. * @returns 汉字数字
  84. * @author xhx
  85. */
  86. public arabicToChinese(num: number): string {
  87. let unit_section = ['', '万', '亿', '万亿', '亿亿'];
  88. let unit_chn = ['', '十', '百', '千'];
  89. let chars = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  90. let unit_index = 0;
  91. let str_ins = '', zh_str = '';
  92. let need_zero = false;
  93. let SectionToChinese = function (section) {
  94. let _zh_str = '';
  95. let _unit_index = 0;
  96. let zero = true;
  97. while (section > 0) {
  98. let v = section % 10;
  99. let _str_ins = '';
  100. if (v === 0) {
  101. if (!zero) {
  102. zero = true;
  103. _zh_str = chars[v] + _zh_str;
  104. }
  105. } else {
  106. zero = false;
  107. if (_unit_index !== 1 || section > 1) {
  108. _str_ins = chars[v];
  109. }
  110. _str_ins += unit_chn[_unit_index];
  111. _zh_str = _str_ins + _zh_str;
  112. }
  113. ++_unit_index;
  114. section = Math.floor(section / 10);
  115. }
  116. return _zh_str;
  117. };
  118. if (num === 0) {
  119. return chars[0];
  120. }
  121. while (num > 0) {
  122. let section = num % 10000;
  123. if (need_zero) {
  124. zh_str = chars[0] + zh_str;
  125. }
  126. str_ins = SectionToChinese(section);
  127. str_ins += (section !== 0) ? unit_section[unit_index] : unit_section[0];
  128. zh_str = str_ins + zh_str;
  129. need_zero = (section < 1000) && (section > 0);
  130. num = Math.floor(num / 10000);
  131. ++unit_index;
  132. }
  133. return zh_str;
  134. }
  135. /**
  136. * 限制字符串长度,超出部分用...代替
  137. * @param str 要拆分的字符串
  138. * @param max_len 最大长度
  139. * @returns 被限制后的字符串
  140. * @author xhx
  141. */
  142. public getNewLimitStr(str: string, max_len: number) {
  143. let arr_str = str.split("");
  144. let new_str = '';
  145. max_len = arr_str.length >= max_len ? max_len : arr_str.length;//字符串不能超出的长度,否则用...显示
  146. for (let i = 0; i < max_len; i++) {
  147. if (i == (max_len - 1)) {
  148. new_str += '...';
  149. } else {
  150. new_str += arr_str[i];
  151. }
  152. }
  153. return new_str;
  154. }
  155. //拼接字符串
  156. //格式例子: format("2021{0}8{1}19", "-", "-");
  157. public static format(originStr: string, ...args: string[])
  158. {
  159. if(args.length > 0)
  160. {
  161. for(let i = 0; i != args.length; ++i)
  162. {
  163. let reg = new RegExp('\\{' + i + '\\}', 'g');
  164. originStr = originStr.replace(reg, args[i]);
  165. }
  166. }
  167. return originStr;
  168. }
  169. }