StringUtil.ts 5.5 KB

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