0e98aa3eea6fd9f9cde7cc7c8d25343191c17afc.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. var guid = "";
  17. for (var i = 1; i <= 32; i++) {
  18. var 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) {
  41. if (fixed === void 0) {
  42. fixed = 2;
  43. }
  44. var k = 1000;
  45. var sizes = ['', 'K', 'M', 'G'];
  46. if (value < k) {
  47. return value.toString();
  48. } else {
  49. var i = Math.floor(Math.log(value) / Math.log(k));
  50. var r = value / Math.pow(k, i);
  51. return r.toFixed(fixed) + sizes[i];
  52. }
  53. }
  54. /**
  55. * 转中文单位计数
  56. * @param value 数字
  57. * @param fixed 保留小数位数
  58. * @example
  59. * 12345 = 1.23万
  60. */
  61. static numberToTenThousand(value, fixed) {
  62. if (fixed === void 0) {
  63. fixed = 2;
  64. }
  65. var k = 10000;
  66. var sizes = ['', '万', '亿', '万亿'];
  67. if (value < k) {
  68. return value.toString();
  69. } else {
  70. var i = Math.floor(Math.log(value) / Math.log(k));
  71. return (value / Math.pow(k, i)).toFixed(fixed) + sizes[i];
  72. }
  73. }
  74. /**
  75. * "," 分割字符串成数组
  76. * @param str 字符串
  77. */
  78. static stringToArray1(str) {
  79. if (str == "") {
  80. return [];
  81. }
  82. return str.split(",");
  83. }
  84. /**
  85. * "|" 分割字符串成数组
  86. * @param str 字符串
  87. */
  88. static stringToArray2(str) {
  89. if (str == "") {
  90. return [];
  91. }
  92. return str.split("|");
  93. }
  94. /**
  95. * ":" 分割字符串成数组
  96. * @param str 字符串
  97. */
  98. static stringToArray3(str) {
  99. if (str == "") {
  100. return [];
  101. }
  102. return str.split(":");
  103. }
  104. /**
  105. * ";" 分割字符串成数组
  106. * @param str 字符串
  107. */
  108. static stringToArray4(str) {
  109. if (str == "") {
  110. return [];
  111. }
  112. return str.split(";");
  113. }
  114. /**
  115. * 字符串截取
  116. * @param str 字符串
  117. * @param n 截取长度
  118. * @param showdot 是否把截取的部分用省略号代替
  119. */
  120. static sub(str, n, showdot) {
  121. if (showdot === void 0) {
  122. showdot = false;
  123. }
  124. var r = /[^\x00-\xff]/g;
  125. if (str.replace(r, "mm").length <= n) {
  126. return str;
  127. }
  128. var m = Math.floor(n / 2);
  129. for (var i = m; i < str.length; i++) {
  130. if (str.substr(0, i).replace(r, "mm").length >= n) {
  131. if (showdot) {
  132. return str.substr(0, i) + "...";
  133. } else {
  134. return str.substr(0, i);
  135. }
  136. }
  137. }
  138. return str;
  139. }
  140. /**
  141. * 计算字符串长度,中文算两个字节
  142. * @param str 字符串
  143. */
  144. static stringLen(str) {
  145. var realLength = 0,
  146. len = str.length,
  147. charCode = -1;
  148. for (var i = 0; i < len; i++) {
  149. charCode = str.charCodeAt(i);
  150. if (charCode >= 0 && charCode <= 128) realLength += 1;else realLength += 2;
  151. }
  152. return realLength;
  153. }
  154. /**
  155. * 是否为空
  156. * @param str
  157. */
  158. static IsEmpty(str) {
  159. return str == null || str == undefined || str.length == 0;
  160. }
  161. /**
  162. * 参数替换
  163. * @param str
  164. * @param rest
  165. *
  166. * @example
  167. *
  168. * var str:string = "here is some info '{0}' and {1}";
  169. * StringUtil.substitute(str, 15.4, true);
  170. *
  171. * "here is some info '15.4' and true"
  172. */
  173. static substitute(str) {
  174. if (str == null) return '';
  175. for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  176. rest[_key - 1] = arguments[_key];
  177. }
  178. var len = rest.length;
  179. var args;
  180. if (len == 1 && rest[0] instanceof Array) {
  181. args = rest[0];
  182. len = args.length;
  183. } else {
  184. args = rest;
  185. }
  186. for (var i = 0; i < len; i++) {
  187. str = str.replace(new RegExp("\\{" + i + "\\}", "g"), args[i]);
  188. }
  189. return str;
  190. }
  191. });
  192. _cclegacy._RF.pop();
  193. _crd = false;
  194. }
  195. };
  196. });
  197. //# sourceMappingURL=0e98aa3eea6fd9f9cde7cc7c8d25343191c17afc.js.map