String字符串工具类

字符串类(StringUtil.cs)

  1 using System;
  2 
  3 namespace Sam.OA.Common
  4 {
  5     /// 
  6     /// 字符处理工具类
  7     /// 作者:陈彦斌
  8     /// 更新时间:2019年9月11日00:07:11
  9     /// 
 10     [Serializable]
 11     public static class StringUtil
 12     {
 13         /// 
 14         /// 判断字符对象为null或者为""
 15         /// 
 16         /// 字符对象
 17         /// 
 18         public static bool isNullOrBlank(Object obj)
 19         {
 20             if (obj == null || obj.ToString().ToLower() == "null" || obj == DBNull.Value || obj.ToString().Trim() == "" || obj.ToString() == "undefined" || obj.ToString().Equals(decimal.MinValue.ToString()))
 21                 return true;
 22             else
 23                 return false;
 24         }
 25         /// 
 26         /// 处理字符串
 27         /// 
 28         /// 字符串
 29         /// 
 30         public static string ProcessString(object obj)
 31         {
 32             return isNullOrBlank(obj) ? "null" : "'" + obj.ToString().Replace("--", "") + "'";
 33         }
 34         /// 
 35         /// 判断字符串是否为日期
 36         /// 
 37         /// 日期字符串
 38         /// 
 39         public static bool ObjectIsDate(object strDate)
 40         {
 41             try
 42             {
 43                 DateTime.Parse(ProcessString(strDate));
 44                 return true;
 45             }
 46             catch
 47             {
 48                 return false;
 49             }
 50         }
 51         /// 
 52         /// 判断字符串是否为decimal
 53         /// 
 54         /// 
 55         /// 
 56         public static bool IsDecimal(object obj)
 57         {
 58             try
 59             {
 60                 decimal.Parse(obj.ToString().Trim());
 61                 return true;
 62             }
 63             catch
 64             {
 65                 return false;
 66             }
 67         }
 68         /// 
 69         /// 对象是否为Null并返回三元运算符值
 70         /// 
 71         /// 对象
 72         /// 
 73         public static string ObjectIsNull(string obj)
 74         {
 75             try
 76             {
 77                 return obj == null ? "" : obj.Replace(" ", "");
 78             }
 79             catch (Exception ex)
 80             {
 81                 return "";
 82             }
 83         }
 84         /// 
 85         /// 对象是否为空并返回三元运算符值
 86         /// 
 87         /// 对象
 88         /// 
 89         public static string ObjectIsBlank(string obj)
 90         {
 91             try
 92             {
 93                 return obj.Replace(" ", "") == "" ? "" : obj.Replace(" ", "");
 94             }
 95             catch (Exception ex)
 96             {
 97                 return "";
 98             }
 99         }
100         /// 
101         /// 对象是否为Decimal并返回三元运算符值
102         /// 
103         /// 对象
104         /// 
105         public static decimal ObjectIsDecimal(object obj)
106         {
107             try
108             {
109                 return Convert.ToDecimal(obj.ToString().Replace(" ", "") == "" ? "0" : obj);
110             }
111             catch (Exception ex)
112             {
113                 return 0;
114             }
115         }
116         /// 
117         /// 获取当前月的第一天
118         /// 
119         /// 
120         public static DateTime GetFirstDayCurrentMonth()
121         {
122             try
123             {
124                 return DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
125             }
126             catch (Exception ex)
127             {
128                 throw ex;
129             }
130         }
131         /// 
132         /// 获取当前月的最后一天
133         /// 
134         /// 
135         public static DateTime GetLastDayCurrentMonth()
136         {
137             try
138             {
139                 return DateTime.Now.AddDays(-DateTime.Now.Day).Date.AddMonths(1);
140             }
141             catch (Exception ex)
142             {
143                 throw ex;
144             }
145         }
146         /// 
147         /// 获取指定日期,在为一年中为第几周
148         /// 
149         /// 指定时间
150         /// 返回第几周,1-52周
151         private static int GetWeekOfYear(DateTime dtWeek)
152         {
153             System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
154             int weekOfYear = gc.GetWeekOfYear(dtWeek, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
155             return weekOfYear;
156         }
157         ///  
158         /// 计算某日起始日期(礼拜一的日期) 
159         ///  
160         /// 该周中任意一天 
161         /// 返回礼拜一日期,无时分秒 
162         public static string GetMondayDate(DateTime someDate)
163         {
164             int i = someDate.DayOfWeek - DayOfWeek.Monday;
165             if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 
166             TimeSpan ts = new TimeSpan(i, 0, 0, 0);
167             return someDate.Subtract(ts).ToShortDateString();
168         }
169         ///  
170         /// 计算某日结束日期(礼拜日的日期) 
171         ///  
172         /// 该周中任意一天 
173         /// 返回礼拜日日期,无时分秒 
174         public static string GetSundayDate(DateTime someDate)
175         {
176             int i = someDate.DayOfWeek - DayOfWeek.Sunday;
177             if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。 
178             TimeSpan ts = new TimeSpan(i, 0, 0, 0);
179             return someDate.Add(ts).ToShortDateString();
180         }
181         /// 
182         /// 获取某月的天数
183         /// 
184         /// 日期
185         /// 当月的天数
186         public static int GetDaysInMonth(DateTime someDate)
187         {
188             return System.Threading.Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInMonth(someDate.Year, someDate.Month);
189         }
190     }
191 }

 

你可能感兴趣的:(String字符串工具类)