StringUtils.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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,fixed:number=1):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(fixed));
  112. return (args + unit);
  113. }
  114. private static SecondsTime:number=1000;
  115. private static MinutesTime:number=60*1000;
  116. private static HoursTime:number=60*60*1000;
  117. private static DayTime:number=24*60*60*1000;
  118. private static MonthTime:number=30*24*60*60*1000;
  119. private static YearTime:number=365*30*24*60*60*1000;
  120. /**
  121. * 时间格式化
  122. * @param time
  123. * @param yearSeparator 年
  124. * @param monthSeparator 月
  125. * @param daySeparator 天
  126. * @param hoursSeparator 时
  127. * @param minutesSeparator 分
  128. * @param secondsSeparator 秒
  129. */
  130. public static TimeFormatting(time:number,yearSeparator:string="y",monthSeparator:string="m",daySeparator:string="d",hoursSeparator:string="h",minutesSeparator:string="m",secondsSeparator:string="s"):string{
  131. let timeStr:string="";
  132. let index:number=0;
  133. //年
  134. let Year:number=0;
  135. while(time>this.YearTime){
  136. time-=this.YearTime;
  137. Year++;
  138. }
  139. if(Year>0){
  140. timeStr+=Year+yearSeparator;
  141. index++;
  142. }
  143. //月
  144. let Month:number=0;
  145. while(time>this.MonthTime){
  146. time-=this.MonthTime;
  147. Month++;
  148. }
  149. if(Month>0){
  150. if(Month<10){
  151. timeStr+="0"+monthSeparator;
  152. }else{
  153. timeStr+=Month+monthSeparator;
  154. }
  155. index++;
  156. }
  157. //天
  158. let Day:number=0;
  159. while (time>this.DayTime) {
  160. time-=this.DayTime;
  161. Day++;
  162. }
  163. if(Day>0){
  164. if(Day<10){
  165. timeStr+="0"+Day+daySeparator;
  166. }else{
  167. timeStr+=Day+daySeparator;
  168. }
  169. index++;
  170. }
  171. //时
  172. if(index<2){
  173. let Hours:number=0;
  174. while (time>this.HoursTime) {
  175. time-=this.HoursTime;
  176. Hours++;
  177. }
  178. if(Hours>0){
  179. if(Hours<10){
  180. timeStr+="0"+Hours+hoursSeparator;
  181. }else{
  182. timeStr+=Hours+hoursSeparator;
  183. }
  184. index++;
  185. }
  186. }else{
  187. return timeStr;
  188. }
  189. //分
  190. if(index<2){
  191. let Minute:number=0;
  192. while (time>this.MinutesTime) {
  193. time-=this.MinutesTime;
  194. Minute++;
  195. }
  196. if(Minute>0){
  197. if(Minute<10){
  198. timeStr+="0"+Minute+minutesSeparator;
  199. }else{
  200. timeStr+=Minute+minutesSeparator;
  201. }
  202. index++;
  203. }
  204. }else{
  205. return timeStr;
  206. }
  207. //秒
  208. if(index<2){
  209. let Seconds:number=0;
  210. while (time>this.SecondsTime) {
  211. time-=this.SecondsTime;
  212. Seconds++;
  213. }
  214. if(Seconds>0){
  215. if(Seconds<10){
  216. timeStr+="0"+Seconds+secondsSeparator;
  217. }else{
  218. timeStr+=Seconds+secondsSeparator;
  219. }
  220. }
  221. }
  222. return timeStr;
  223. }
  224. /**
  225. * 获取文件后缀名
  226. * @param url
  227. */
  228. static getFileSuffix(url: string): string {
  229. let index:number=url.lastIndexOf(".");
  230. if(index<0){
  231. throw new Error(url+"没有后缀!!!");
  232. }
  233. let suixx: string = url.substring(index+1);
  234. return suixx.toLocaleUpperCase();
  235. }
  236. /**
  237. * 更换后缀
  238. */
  239. static changeFileSuffix(url: string, suff: string): string {
  240. let index:number=url.lastIndexOf(".");
  241. if(index<0){
  242. throw new Error(url+"没有后缀!!!");
  243. }
  244. let suixx:string = url.substring(index+1);
  245. let changeUrl:string = url.replace(suixx,suff);
  246. return changeUrl
  247. }
  248. /**
  249. * 换行文本
  250. */
  251. static getLineText(str:string):string{
  252. let text:string = "";
  253. let tmp:string[] = str.split("&");
  254. for(let i:number=0; i<tmp.length;i++){
  255. text = text + tmp[i];
  256. if(i != tmp.length-1){
  257. text = text+"\n"
  258. }
  259. }
  260. return text;
  261. }
  262. }