StringUtils.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { DataModelEventType } from "../models/DataModelEventType";
  2. export default class StringUtils
  3. {
  4. /**
  5. * 是否为空
  6. * @param str
  7. */
  8. public static isEmpty(str:string):boolean{
  9. if(str==null||str==undefined||str.length==0){
  10. return true;
  11. }
  12. return false;
  13. }
  14. /**
  15. * 替换全部字符串
  16. * @param string src 源串
  17. * @param string from_ch 被替换的字符
  18. * @param string to_ch 替换的字符
  19. *
  20. * @return string 结果字符串
  21. */
  22. public static replaceAll(src:string,from_ch:string,to_ch:string):string
  23. {
  24. return src.split(from_ch).join(to_ch);
  25. }
  26. /**
  27. * 参数替换
  28. * @param str
  29. * @param rest
  30. *
  31. * @example
  32. *
  33. * var str:string = "here is some info '{0}' and {1}";
  34. * trace(StringUtil.substitute(str, 15.4, true));
  35. *
  36. * // this will output the following string:
  37. * // "here is some info '15.4' and true"
  38. */
  39. public static substitute(str:string, ... rest):string
  40. {
  41. if (str == null) return '';
  42. // Replace all of the parameters in the msg string.
  43. var len:number = rest.length;
  44. var args:any[];
  45. if (len == 1 && rest[0] instanceof Array)
  46. {
  47. args = rest[0];
  48. len = args.length;
  49. }
  50. else
  51. {
  52. args = rest;
  53. }
  54. for (var i:number = 0; i < len; i++)
  55. {
  56. str = str.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
  57. }
  58. return str;
  59. }
  60. /**
  61. * 拆分字符串
  62. * @param str
  63. */
  64. public static splitString(str:string):string[][]{
  65. let args:string[][] = new Array<string[]>();
  66. let tmp:string[] = str.split(",");
  67. tmp.forEach((val, key) => {
  68. let s:string[] = val.split(":");
  69. args.push(s);
  70. });
  71. return args;
  72. }
  73. /**
  74. * 转换成万,千万
  75. * @param num
  76. */
  77. public static numberUtilsTenThousand(num:number,fixed:number=1):string{
  78. let args:string;
  79. let unit:string;
  80. if(num < 10000){
  81. unit = "";
  82. }else if(num < 10000000){
  83. num = num / 10000;
  84. unit = "万";
  85. }else{
  86. num = num / 10000000;
  87. unit = "千万";
  88. }
  89. args = String(num.toFixed(fixed));
  90. return (args + unit);
  91. }
  92. /**
  93. * 转换为KMB 10100 = 10.1k
  94. * @param num
  95. */
  96. public static numberUtilsEn(num:number):string{
  97. let args:string;
  98. let unit:string;
  99. if(num < 10000){
  100. unit = "";
  101. }else if(num < 10000000){
  102. num = num / 1000;
  103. unit = "K";
  104. }else if(num < 100000000000){
  105. num = num / 1000000;
  106. unit = "M";
  107. }else{
  108. num = num / 10000000000;
  109. unit = "B";
  110. }
  111. args = String(num.toFixed(1));
  112. return (args + unit);
  113. }
  114. private static DayTime:number=24*60*60*1000;
  115. private static HoursTime:number=60*60*1000;
  116. private static MinutesTime:number=60*1000;
  117. private static SecondsTime:number=1000;
  118. /**
  119. * 时间格式化
  120. * @param time
  121. * @param daySeparator
  122. * @param hoursSeparator
  123. * @param minutesSeparator
  124. * @param secondsSeparator
  125. */
  126. public static TimeFormatting(time:number,daySeparator:string="d",hoursSeparator:string="h",minutesSeparator:string="m",secondsSeparator:string="s"):string{
  127. let timeStr:string="";
  128. let index:number=0;
  129. //天
  130. let Day:number=0;
  131. while (time>this.DayTime) {
  132. time-=this.DayTime;
  133. Day++;
  134. }
  135. if(Day>0){
  136. if(Day<10){
  137. timeStr+="0"+Day+daySeparator;
  138. }else{
  139. timeStr+=Day+daySeparator;
  140. }
  141. index++;
  142. }
  143. //时
  144. let Hours:number=0;
  145. while (time>this.HoursTime) {
  146. time-=this.HoursTime;
  147. Hours++;
  148. }
  149. if(Hours>0){
  150. if(Hours<10){
  151. timeStr+="0"+Hours+hoursSeparator;
  152. }else{
  153. timeStr+=Hours+hoursSeparator;
  154. }
  155. index++;
  156. }
  157. //分
  158. let Minute:number=0;
  159. while (time>this.MinutesTime) {
  160. time-=this.MinutesTime;
  161. Minute++;
  162. }
  163. if(Minute>0){
  164. if(Minute<10){
  165. timeStr+="0"+Minute+minutesSeparator;
  166. }else{
  167. timeStr+=Minute+minutesSeparator;
  168. }
  169. index++;
  170. }
  171. //秒
  172. let Seconds:number=0;
  173. while (time>this.SecondsTime) {
  174. time-=this.SecondsTime;
  175. Seconds++;
  176. }
  177. if(Seconds>0&&index<2){
  178. if(Seconds<10){
  179. timeStr+="0"+Seconds+secondsSeparator;
  180. }else{
  181. timeStr+=Seconds+secondsSeparator;
  182. }
  183. }
  184. return timeStr;
  185. }
  186. /**
  187. * 获取文件后缀名
  188. * @param url
  189. */
  190. static getFileSuffix(url: string): string {
  191. let index:number=url.lastIndexOf(".");
  192. if(index<0){
  193. throw new Error(url+"没有后缀!!!");
  194. }
  195. let suixx: string = url.substring(index+1);
  196. return suixx.toLocaleUpperCase();
  197. }
  198. /**
  199. * 更换后缀
  200. */
  201. static changeFileSuffix(url: string, suff: string): string {
  202. let index:number=url.lastIndexOf(".");
  203. if(index<0){
  204. throw new Error(url+"没有后缀!!!");
  205. }
  206. let suixx:string = url.substring(index+1);
  207. let changeUrl:string = url.replace(suixx,suff);
  208. return changeUrl
  209. }
  210. /**
  211. * 换行文本
  212. */
  213. static getLineText(str:string):string{
  214. let text:string = "";
  215. let tmp:string[] = str.split("&");
  216. for(let i:number=0; i<tmp.length;i++){
  217. text = text + tmp[i];
  218. if(i != tmp.length-1){
  219. text = text+"\n"
  220. }
  221. }
  222. return text;
  223. }
  224. }