StringUtils.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * 说明:字符串工具类
  3. */
  4. /**
  5. * 得到字符串含有某个字符的个数
  6. * @param str 字符串
  7. * @param char 某个字符
  8. * @returns 个数
  9. */
  10. function getCharCount(str: string, char: string) : number {
  11. const regex = new RegExp(char, 'g'); // 使用g表示整个字符串都要匹配
  12. const result = str.match(regex); //match方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
  13. const count=!result ? 0 : result.length;
  14. return count;
  15. }
  16. /**
  17. * 判断字符串是否是 Base64 编码
  18. * @param {String} str
  19. */
  20. function isBase64(str: string) : boolean {
  21. return /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/.test(str);
  22. }
  23. /**
  24. * 检测字符串是否是一串数字
  25. * @param {String} val
  26. */
  27. function isNumber(val: string) : boolean {
  28. const regPos = /^\d+(\.\d+)?$/; //非负浮点数
  29. const regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
  30. if (regPos.test(val) || regNeg.test(val)) {
  31. return true;
  32. } else {
  33. return false;
  34. }
  35. }
  36. /**
  37. * 检查字符串是否是中国的11位手机号
  38. * @param str 字符串
  39. */
  40. function isChinaPoneNumber(str: string) : boolean {
  41. if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(str)) {
  42. return false;
  43. } else {
  44. return true;
  45. }
  46. }
  47. /**
  48. * 检查字符串是否是邮箱
  49. * @param str 字符串
  50. */
  51. function isEmail(str: string) : boolean {
  52. const re = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  53. if (re.test(str) !== true) {
  54. return false;
  55. }else{
  56. return true;
  57. }
  58. }
  59. /**
  60. * 将字符串转为16进制字符串
  61. * @param str 字符串
  62. */
  63. function strToHexCharCode(str: string, with0x = true): string {
  64. if(str === "")
  65. return "";
  66. const hexCharCode = [];
  67. if(with0x) hexCharCode.push("0x");
  68. for(let i = 0; i < str.length; i++) {
  69. hexCharCode.push((str.charCodeAt(i)).toString(16));
  70. }
  71. return hexCharCode.join("");
  72. }
  73. /**
  74. * 数字补0
  75. * @param num 数字
  76. * @param n 如果数字不足n位,则自动补0
  77. */
  78. function pad(num: number, n: number) : string {
  79. let strNum = num.toString();
  80. let len = strNum.length;
  81. while (len < n) {
  82. strNum = "0" + strNum;
  83. len++;
  84. }
  85. return strNum;
  86. }
  87. /**
  88. * 按千位逗号分割
  89. * @param s 需要格式化的数值.
  90. * @param type 判断格式化后是否需要小数位.
  91. */
  92. function formatNumberWithComma(s: string, addComma: boolean) : string {
  93. if (/[^0-9]/.test(s))
  94. return "0";
  95. if (s === null || s === "")
  96. return "0";
  97. s = s.toString().replace(/^(\d*)$/, "$1.");
  98. s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
  99. s = s.replace(".", ",");
  100. const re = /(\d)(\d{3},)/;
  101. while (re.test(s))
  102. s = s.replace(re, "$1,$2");
  103. s = s.replace(/,(\d\d)$/, ".$1");
  104. if (!addComma) { // 不带小数位(默认是有小数位)
  105. const a = s.split(".");
  106. if (a[1] === "00") {
  107. s = a[0];
  108. }
  109. }
  110. return s;
  111. }
  112. /**
  113. * 格式化显示大数字
  114. * @param Number 数字
  115. */
  116. function formatHugeNumber(Number: number) : string {
  117. if (Number >= 1000000000000)
  118. return Number.toExponential(2).replace(/e\+/g, 'x10^');
  119. if (Number >= 1000000000)
  120. return (Number / 1000000000).toFixed(2) + 'B';
  121. if (Number >= 1000000)
  122. return (Number / 1000000).toFixed(2) + 'M';
  123. return Number.toFixed(2);
  124. }
  125. const StringUtils = {
  126. formatDate(date: Date, formatStr = "YYYY-MM-dd HH:mm:ss") : string {
  127. let str = formatStr ? formatStr : "YYYY-MM-dd HH:mm:ss";
  128. //let Week = ['日','一','二','三','四','五','六'];
  129. str = str.replace(/yyyy|YYYY/, date.getFullYear().toString());
  130. str = str.replace(/MM/, pad(date.getMonth() + 1, 2));
  131. str = str.replace(/M/, (date.getMonth() + 1).toString());
  132. str = str.replace(/dd|DD/, pad(date.getDate(), 2));
  133. str = str.replace(/d/, date.getDate().toString());
  134. str = str.replace(/HH/, pad(date.getHours(), 2));
  135. str = str.replace(
  136. /hh/,
  137. pad(date.getHours() > 12 ? date.getHours() - 12 : date.getHours(), 2)
  138. );
  139. str = str.replace(/mm/, pad(date.getMinutes(), 2));
  140. str = str.replace(/ii/, pad(date.getMinutes(), 2));
  141. str = str.replace(/ss/, pad(date.getSeconds(), 2));
  142. return str;
  143. },
  144. /**
  145. * 字符串判空
  146. * @param str 字符串
  147. */
  148. isNullOrEmpty(str: string | undefined | null | false) : boolean {
  149. return !str || typeof str === 'undefined' || str === ''
  150. },
  151. isBase64,
  152. isNumber,
  153. isChinaPoneNumber,
  154. isEmail,
  155. strToHexCharCode,
  156. pad,
  157. formatHugeNumber,
  158. formatNumberWithComma,
  159. getFileName(path: string) : string {
  160. let pos = path.lastIndexOf('/');
  161. if(pos < 0) pos = path.lastIndexOf('\\');
  162. return path.substring(pos + 1);
  163. },
  164. getFileExt(path: string) : string {
  165. return path.substring(path.lastIndexOf('.') + 1);
  166. },
  167. getCharCount,
  168. getFileSizeStringAuto(filesize: number) : string {
  169. let sizeStr = '';
  170. if(filesize >= 1073741824){
  171. filesize = Math.round(filesize/1073741824*100)/100;
  172. sizeStr = filesize + "GB";
  173. }else if(filesize >= 1048576) {
  174. filesize = Math.round(filesize/1048576*100)/100;
  175. sizeStr = filesize + "MB";
  176. }else{
  177. filesize = Math.round(filesize/1024*100)/100;
  178. sizeStr = filesize + "KB";
  179. }
  180. return sizeStr;
  181. },
  182. /**
  183. * 移除URL的地址部分,只保留路径
  184. * @param str 原URL
  185. * @returns
  186. */
  187. removeUrlOrigin(str: string) : string {
  188. if(str.startsWith('http://') || str.startsWith('https://') || str.startsWith('fts://') || str.startsWith('ftps://')) {
  189. str = str.substr(str.indexOf('://') + 3);
  190. str = str.substr(str.indexOf('/') + 1);
  191. }
  192. return str;
  193. },
  194. /**
  195. * 将手机号转换成 xxx******xx
  196. * @param str 手机号
  197. */
  198. convertPhoneToSecret6(str: string): string{
  199. return str.replace(/^(\d{3})(\d*)(\d{2}$)/, "$1******$3");
  200. },
  201. /**
  202. * 将手机号转换成 尾号xxxx用户
  203. * @param str 手机号
  204. */
  205. convertPhoneToUserName(str: string): string{
  206. return '尾号' + str.substring(str.length - 4) + '用户';
  207. },
  208. }
  209. export default StringUtils;