123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- export default class StringUtils
- {
- /**
- * 是否为空
- * @param str
- */
- public static isEmpty(str:string):boolean{
- if(str==null||str==undefined||str.length==0){
- return true;
- }
- return false;
- }
- /**
- * 替换全部字符串
- * @param string src 源串
- * @param string from_ch 被替换的字符
- * @param string to_ch 替换的字符
- *
- * @return string 结果字符串
- */
- public static replaceAll(src:string,from_ch:string,to_ch:string):string
- {
- return src.split(from_ch).join(to_ch);
- }
- /**
- * 参数替换
- * @param str
- * @param rest
- *
- * @example
- *
- * var str:string = "here is some info '{0}' and {1}";
- * trace(StringUtil.substitute(str, 15.4, true));
- *
- * // this will output the following string:
- * // "here is some info '15.4' and true"
- */
- public static substitute(str:string, ... rest):string
- {
- if (str == null) return '';
-
- // Replace all of the parameters in the msg string.
- var len:number = rest.length;
- var args:any[];
- if (len == 1 && rest[0] instanceof Array)
- {
- args = rest[0];
- len = args.length;
- }
- else
- {
- args = rest;
- }
-
- for (var i:number = 0; i < len; i++)
- {
- str = str.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
- }
-
- return str;
- }
- /**
- * 拆分字符串
- * @param str
- */
- public static splitString(str:string):string[][]{
- let args:string[][] = new Array<string[]>();
- let tmp:string[] = str.split(",");
- tmp.forEach((val, key) => {
- let s:string[] = val.split(":");
- args.push(s);
- });
- return args;
- }
- /**
- * 转换成万,千万
- * @param num
- */
- public static numberUtilsTenThousand(num:number,fixed:number=1):string{
- let args:string;
- let unit:string;
- if(num < 10000){
- unit = "";
- }else if(num < 10000000){
- num = num / 10000;
- unit = "万";
- }else{
- num = num / 10000000;
- unit = "千万";
- }
- args = String(num.toFixed(fixed));
- return (args + unit);
- }
- /**
- * 转换为KMB 10100 = 10.1k
- * @param num
- */
- public static numberUtilsEn(num:number,fixed:number=1):string{
- let args:string;
- let unit:string;
- if(num < 10000){
- unit = "";
- }else if(num < 10000000){
- num = num / 1000;
- unit = "K";
- }else if(num < 100000000000){
- num = num / 1000000;
- unit = "M";
- }else{
- num = num / 10000000000;
- unit = "B";
- }
- args = String(num.toFixed(fixed));
- return (args + unit);
- }
- private static SecondsTime:number=1000;
- private static MinutesTime:number=60*1000;
- private static HoursTime:number=60*60*1000;
- private static DayTime:number=24*60*60*1000;
- private static MonthTime:number=30*24*60*60*1000;
- private static YearTime:number=365*30*24*60*60*1000;
-
-
- /**
- * 时间格式化
- * @param time
- * @param yearSeparator 年
- * @param monthSeparator 月
- * @param daySeparator 天
- * @param hoursSeparator 时
- * @param minutesSeparator 分
- * @param secondsSeparator 秒
- */
- public static TimeFormatting(time:number,yearSeparator:string="y",monthSeparator:string="m",daySeparator:string="d",hoursSeparator:string="h",minutesSeparator:string="m",secondsSeparator:string="s"):string{
- let timeStr:string="";
- let index:number=0;
- //年
- let Year:number=0;
- while(time>this.YearTime){
- time-=this.YearTime;
- Year++;
- }
- if(Year>0){
- timeStr+=Year+yearSeparator;
- index++;
- }
- //月
- let Month:number=0;
- while(time>this.MonthTime){
- time-=this.MonthTime;
- Month++;
- }
- if(Month>0){
- if(Month<10){
- timeStr+="0"+monthSeparator;
- }else{
- timeStr+=Month+monthSeparator;
- }
- index++;
- }
- //天
- let Day:number=0;
- while (time>this.DayTime) {
- time-=this.DayTime;
- Day++;
- }
- if(Day>0){
- if(Day<10){
- timeStr+="0"+Day+daySeparator;
- }else{
- timeStr+=Day+daySeparator;
- }
- index++;
- }
- //时
- if(index<2){
- let Hours:number=0;
- while (time>this.HoursTime) {
- time-=this.HoursTime;
- Hours++;
- }
- if(Hours>0){
- if(Hours<10){
- timeStr+="0"+Hours+hoursSeparator;
- }else{
- timeStr+=Hours+hoursSeparator;
- }
- index++;
- }
- }else{
- return timeStr;
- }
- //分
- if(index<2){
- let Minute:number=0;
- while (time>this.MinutesTime) {
- time-=this.MinutesTime;
- Minute++;
- }
- if(Minute>0){
- if(Minute<10){
- timeStr+="0"+Minute+minutesSeparator;
- }else{
- timeStr+=Minute+minutesSeparator;
- }
- index++;
- }
- }else{
- return timeStr;
- }
- //秒
- if(index<2){
- let Seconds:number=0;
- while (time>this.SecondsTime) {
- time-=this.SecondsTime;
- Seconds++;
- }
- if(Seconds>0){
- if(Seconds<10){
- timeStr+="0"+Seconds+secondsSeparator;
- }else{
- timeStr+=Seconds+secondsSeparator;
- }
- }
- }
- return timeStr;
- }
- /**
- * 获取文件后缀名
- * @param url
- */
- static getFileSuffix(url: string): string {
- let index:number=url.lastIndexOf(".");
- if(index<0){
- throw new Error(url+"没有后缀!!!");
- }
- let suixx: string = url.substring(index+1);
- return suixx.toLocaleUpperCase();
- }
- /**
- * 更换后缀
- */
- static changeFileSuffix(url: string, suff: string): string {
- let index:number=url.lastIndexOf(".");
- if(index<0){
- throw new Error(url+"没有后缀!!!");
- }
- let suixx:string = url.substring(index+1);
- let changeUrl:string = url.replace(suixx,suff);
- return changeUrl
- }
- /**
- * 换行文本
- */
- static getLineText(str:string):string{
- let text:string = "";
- let tmp:string[] = str.split("&");
- for(let i:number=0; i<tmp.length;i++){
- text = text + tmp[i];
- if(i != tmp.length-1){
- text = text+"\n"
- }
- }
- return text;
- }
- }
|