0e98aa3eea6fd9f9cde7cc7c8d25343191c17afc.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, StringUtil, _crd;
  4. _export("StringUtil", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "aa8435LSBtAR5HPsje6IJ2w", "StringUtil", undefined);
  12. /** 字符串工具 */
  13. _export("StringUtil", StringUtil = class StringUtil {
  14. /** 获取一个唯一标识的字符串 */
  15. static guid() {
  16. let guid = "";
  17. for (let i = 1; i <= 32; i++) {
  18. let n = Math.floor(Math.random() * 16.0).toString(16);
  19. guid += n;
  20. if (i == 8 || i == 12 || i == 16 || i == 20) guid += "-";
  21. }
  22. return guid;
  23. }
  24. /**
  25. * 转美式计数字符串
  26. * @param value 数字
  27. * @example
  28. * 123456789 = 123,456,789
  29. */
  30. static numberTotPermil(value) {
  31. return value.toLocaleString();
  32. }
  33. /**
  34. * 转英文单位计数
  35. * @param value 数字
  36. * @param fixed 保留小数位数
  37. * @example
  38. * 12345 = 12.35K
  39. */
  40. static numberToThousand(value, fixed = 2) {
  41. const k = 1000;
  42. const sizes = ['', 'K', 'M', 'G'];
  43. if (value < k) {
  44. return value.toString();
  45. } else {
  46. const i = Math.floor(Math.log(value) / Math.log(k));
  47. const r = value / Math.pow(k, i);
  48. return r.toFixed(fixed) + sizes[i];
  49. }
  50. }
  51. /**
  52. * 转中文单位计数
  53. * @param value 数字
  54. * @param fixed 保留小数位数
  55. * @example
  56. * 12345 = 1.23万
  57. */
  58. static numberToTenThousand(value, fixed = 2) {
  59. const k = 10000;
  60. const sizes = ['', '万', '亿', '万亿'];
  61. if (value < k) {
  62. return value.toString();
  63. } else {
  64. const i = Math.floor(Math.log(value) / Math.log(k));
  65. return (value / Math.pow(k, i)).toFixed(fixed) + sizes[i];
  66. }
  67. }
  68. /**
  69. * "," 分割字符串成数组
  70. * @param str 字符串
  71. */
  72. static stringToArray1(str) {
  73. if (str == "") {
  74. return [];
  75. }
  76. return str.split(",");
  77. }
  78. /**
  79. * "|" 分割字符串成数组
  80. * @param str 字符串
  81. */
  82. static stringToArray2(str) {
  83. if (str == "") {
  84. return [];
  85. }
  86. return str.split("|");
  87. }
  88. /**
  89. * ":" 分割字符串成数组
  90. * @param str 字符串
  91. */
  92. static stringToArray3(str) {
  93. if (str == "") {
  94. return [];
  95. }
  96. return str.split(":");
  97. }
  98. /**
  99. * ";" 分割字符串成数组
  100. * @param str 字符串
  101. */
  102. static stringToArray4(str) {
  103. if (str == "") {
  104. return [];
  105. }
  106. return str.split(";");
  107. }
  108. /**
  109. * 字符串截取
  110. * @param str 字符串
  111. * @param n 截取长度
  112. * @param showdot 是否把截取的部分用省略号代替
  113. */
  114. static sub(str, n, showdot = false) {
  115. const r = /[^\x00-\xff]/g;
  116. if (str.replace(r, "mm").length <= n) {
  117. return str;
  118. }
  119. const m = Math.floor(n / 2);
  120. for (let i = m; i < str.length; i++) {
  121. if (str.substr(0, i).replace(r, "mm").length >= n) {
  122. if (showdot) {
  123. return str.substr(0, i) + "...";
  124. } else {
  125. return str.substr(0, i);
  126. }
  127. }
  128. }
  129. return str;
  130. }
  131. /**
  132. * 计算字符串长度,中文算两个字节
  133. * @param str 字符串
  134. */
  135. static stringLen(str) {
  136. let realLength = 0,
  137. len = str.length,
  138. charCode = -1;
  139. for (let i = 0; i < len; i++) {
  140. charCode = str.charCodeAt(i);
  141. if (charCode >= 0 && charCode <= 128) realLength += 1;else realLength += 2;
  142. }
  143. return realLength;
  144. }
  145. /**
  146. * 是否为空
  147. * @param str
  148. */
  149. static IsEmpty(str) {
  150. return str == null || str == undefined || str.length == 0;
  151. }
  152. /**
  153. * 参数替换
  154. * @param str
  155. * @param rest
  156. *
  157. * @example
  158. *
  159. * var str:string = "here is some info '{0}' and {1}";
  160. * StringUtil.substitute(str, 15.4, true);
  161. *
  162. * "here is some info '15.4' and true"
  163. */
  164. static substitute(str, ...rest) {
  165. if (str == null) return '';
  166. let len = rest.length;
  167. let args;
  168. if (len == 1 && rest[0] instanceof Array) {
  169. args = rest[0];
  170. len = args.length;
  171. } else {
  172. args = rest;
  173. }
  174. for (let i = 0; i < len; i++) {
  175. str = str.replace(new RegExp("\\{" + i + "\\}", "g"), args[i]);
  176. }
  177. return str;
  178. }
  179. });
  180. _cclegacy._RF.pop();
  181. _crd = false;
  182. }
  183. };
  184. });
  185. //# sourceMappingURL=0e98aa3eea6fd9f9cde7cc7c8d25343191c17afc.js.map