StringUtils.ts 7.1 KB

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