1. 程式人生 > >【C#公共幫助類】 ToolsHelper幫助類

【C#公共幫助類】 ToolsHelper幫助類

  1 using System;
  2 using System.Text;
  3 using System.Text.RegularExpressions;
  4 using System.Collections.Generic;
  5 using System.Reflection;
  6 using System.Web;
  7 using System.Web.Mvc;
  8 using System.ComponentModel;
  9 
 10 namespace Common
 11 {
 12     /// <summary>
 13
/// 功能描述:共用工具類 14 /// </summary> 15 public static class Tools 16 { 17 18 #region 得到字串長度,一個漢字長度為2 19 /// <summary> 20 /// 得到字串長度,一個漢字長度為2 21 /// </summary> 22 /// <param name="inputString">引數字串</param> 23
/// <returns></returns> 24 public static int StrLength(string inputString) 25 { 26 System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); 27 int tempLen = 0; 28 byte[] s = ascii.GetBytes(inputString); 29 for
(int i = 0; i < s.Length; i++) 30 { 31 if ((int)s[i] == 63) 32 tempLen += 2; 33 else 34 tempLen += 1; 35 } 36 return tempLen; 37 } 38 #endregion 39 40 #region 擷取指定長度字串 41 /// <summary> 42 /// 擷取指定長度字串 43 /// </summary> 44 /// <param name="inputString">要處理的字串</param> 45 /// <param name="len">指定長度</param> 46 /// <returns>返回處理後的字串</returns> 47 public static string ClipString(string inputString, int len) 48 { 49 bool isShowFix = false; 50 if (len % 2 == 1) 51 { 52 isShowFix = true; 53 len--; 54 } 55 System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); 56 int tempLen = 0; 57 string tempString = ""; 58 byte[] s = ascii.GetBytes(inputString); 59 for (int i = 0; i < s.Length; i++) 60 { 61 if ((int)s[i] == 63) 62 tempLen += 2; 63 else 64 tempLen += 1; 65 66 try 67 { 68 tempString += inputString.Substring(i, 1); 69 } 70 catch 71 { 72 break; 73 } 74 75 if (tempLen > len) 76 break; 77 } 78 79 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); 80 if (isShowFix && mybyte.Length > len) 81 tempString += ""; 82 return tempString; 83 } 84 #endregion 85 86 #region 獲得兩個日期的間隔 87 /// <summary> 88 /// 獲得兩個日期的間隔 89 /// </summary> 90 /// <param name="DateTime1">日期一。</param> 91 /// <param name="DateTime2">日期二。</param> 92 /// <returns>日期間隔TimeSpan。</returns> 93 public static TimeSpan DateDiff(DateTime DateTime1, DateTime DateTime2) 94 { 95 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); 96 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); 97 TimeSpan ts = ts1.Subtract(ts2).Duration(); 98 return ts; 99 } 100 #endregion 101 102 #region 格式化日期時間 103 /// <summary> 104 /// 格式化日期時間 105 /// </summary> 106 /// <param name="dateTime1">日期時間</param> 107 /// <param name="dateMode">顯示模式</param> 108 /// <returns>0-9種模式的日期</returns> 109 public static string FormatDate(DateTime dateTime1, string dateMode) 110 { 111 switch (dateMode) 112 { 113 case "0": 114 return dateTime1.ToString("yyyy-MM-dd"); 115 case "1": 116 return dateTime1.ToString("yyyy-MM-dd HH:mm:ss"); 117 case "2": 118 return dateTime1.ToString("yyyy/MM/dd"); 119 case "3": 120 return dateTime1.ToString("yyyy年MM月dd日"); 121 case "4": 122 return dateTime1.ToString("MM-dd"); 123 case "5": 124 return dateTime1.ToString("MM/dd"); 125 case "6": 126 return dateTime1.ToString("MM月dd日"); 127 case "7": 128 return dateTime1.ToString("yyyy-MM"); 129 case "8": 130 return dateTime1.ToString("yyyy/MM"); 131 case "9": 132 return dateTime1.ToString("yyyy年MM月"); 133 default: 134 return dateTime1.ToString(); 135 } 136 } 137 #endregion 138 139 #region 得到隨機日期 140 /// <summary> 141 /// 得到隨機日期 142 /// </summary> 143 /// <param name="time1">起始日期</param> 144 /// <param name="time2">結束日期</param> 145 /// <returns>間隔日期之間的 隨機日期</returns> 146 public static DateTime GetRandomTime(DateTime time1, DateTime time2) 147 { 148 Random random = new Random(); 149 DateTime minTime = new DateTime(); 150 DateTime maxTime = new DateTime(); 151 152 System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks); 153 154 // 獲取兩個時間相隔的秒數 155 double dTotalSecontds = ts.TotalSeconds; 156 int iTotalSecontds = 0; 157 158 if (dTotalSecontds > System.Int32.MaxValue) 159 { 160 iTotalSecontds = System.Int32.MaxValue; 161 } 162 else if (dTotalSecontds < System.Int32.MinValue) 163 { 164 iTotalSecontds = System.Int32.MinValue; 165 } 166 else 167 { 168 iTotalSecontds = (int)dTotalSecontds; 169 } 170 171 172 if (iTotalSecontds > 0) 173 { 174 minTime = time2; 175 maxTime = time1; 176 } 177 else if (iTotalSecontds < 0) 178 { 179 minTime = time1; 180 maxTime = time2; 181 } 182 else 183 { 184 return time1; 185 } 186 187 int maxValue = iTotalSecontds; 188 189 if (iTotalSecontds <= System.Int32.MinValue) 190 maxValue = System.Int32.MinValue + 1; 191 192 int i = random.Next(System.Math.Abs(maxValue)); 193 194 return minTime.AddSeconds(i); 195 } 196 /// <summary> 197 /// 獲取時間戳 198 /// </summary> 199 public static string GetRandomTimeSpan() 200 { 201 TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); 202 return Convert.ToInt64(ts.TotalSeconds).ToString(); 203 } 204 #endregion 205 206 #region HTML轉行成TEXT 207 /// <summary> 208 /// HTML轉行成TEXT 209 /// </summary> 210 /// <param name="strHtml"></param> 211 /// <returns></returns> 212 public static string HtmlToTxt(string strHtml) 213 { 214 string[] aryReg ={ 215 @"<script[^>]*?>.*?</script>", 216 @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", 217 @"([\r\n])[\s]+", 218 @"&(quot|#34);", 219 @"&(amp|#38);", 220 @"&(lt|#60);", 221 @"&(gt|#62);", 222 @"&(nbsp|#160);", 223 @"&(iexcl|#161);", 224 @"&(cent|#162);", 225 @"&(pound|#163);", 226 @"&(copy|#169);", 227 @"&#(\d+);", 228 @"-->", 229 @"<!--.*\n" 230 }; 231 232 string newReg = aryReg[0]; 233 string strOutput = strHtml; 234 for (int i = 0; i < aryReg.Length; i++) 235 { 236 Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase); 237 strOutput = regex.Replace(strOutput, string.Empty); 238 } 239 240 strOutput.Replace("<", ""); 241 strOutput.Replace(">", ""); 242 strOutput.Replace("\r\n", ""); 243 244 245 return strOutput; 246 } 247 #endregion 248 249 #region 判斷物件是否為空 250 /// <summary> 251 /// 判斷物件是否為空,為空返回true 252 /// </summary> 253 /// <typeparam name="T">要驗證的物件的型別</typeparam> 254 /// <param name="data">要驗證的物件</param> 255 public static bool IsNullOrEmpty<T>(this T data) 256 { 257 //如果為null 258 if (data == null) 259 { 260 return true; 261 } 262 263 //如果為"" 264 if (data.GetType() == typeof(String)) 265 { 266 if (string.IsNullOrEmpty(data.ToString().Trim()) || data.ToString() == "") 267 { 268 return true; 269 } 270 } 271 272 //如果為DBNull 273 if (data.GetType() == typeof(DBNull)) 274 { 275 return true; 276 } 277 278 //不為空 279 return false; 280 } 281 282 /// <summary> 283 /// 判斷物件是否為空,為空返回true 284 /// </summary> 285 /// <param name="data">要驗證的物件</param> 286 public static bool IsNullOrEmpty(this object data) 287 { 288 //如果為null 289 if (data == null) 290 { 291 return true; 292 } 293 294 //如果為"" 295 if (data.GetType() == typeof(String)) 296 { 297 if (string.IsNullOrEmpty(data.ToString().Trim())) 298 { 299 return true; 300 } 301 } 302 303 //如果為DBNull 304 if (data.GetType() == typeof(DBNull)) 305 { 306 return true; 307 } 308 309 //不為空 310 return false; 311 } 312 #endregion 313 314 #region 驗證是否為浮點數 315 /// <summary> 316 /// 驗證是否浮點數 317 /// </summary> 318 /// <param name="floatNum"></param> 319 /// <returns></returns> 320 public static bool IsFloat(this string floatNum) 321 { 322 //如果為空,認為驗證不合格 323 if (IsNullOrEmpty(floatNum)) 324 { 325 return false; 326 } 327 //清除要驗證字串中的空格 328 floatNum = floatNum.Trim(); 329 330 //模式字串 331 string pattern = @"^(-?\d+)(\.\d+)?$"; 332 333 //驗證 334 return RegexHelper.IsMatch(floatNum, pattern); 335 } 336 #endregion 337 338 #region 驗證是否為整數 339 /// <summary> 340 /// 驗證是否為整數 如果為空,認為驗證不合格 返回false 341 /// </summary> 342 /// <param name="number">要驗證的整數</param> 343 public static bool IsInt(this string number) 344 { 345 //如果為空,認為驗證不合格 346 if (IsNullOrEmpty(number)) 347 { 348 return false; 349 } 350 351 //清除要驗證字串中的空格 352 number = number.Trim(); 353 354 //模式字串 355 string pattern = @"^[0-9]+[0-9]*$"; 356 357 //驗證 358 return RegexHelper.IsMatch(number, pattern); 359 } 360 #endregion 361 362 #region 驗證是否為數字 363 /// <summary> 364 /// 驗證是否為數字 365 /// </summary> 366 /// <param name="number">要驗證的數字</param> 367 public static bool IsNumber(this string number) 368 { 369 //如果為空,認為驗證不合格 370 if (IsNullOrEmpty(number)) 371 { 372 return false; 373 } 374 375 //清除要驗證字串中的空格 376 number = number.Trim(); 377 378 //模式字串 379 string pattern = @"^[0-9]+[0-9]*[.]?[0-9]*$"; 380 381 //驗證 382 return RegexHelper.IsMatch(number, pattern); 383 } 384 #endregion 385 386 #region 驗證日期是否合法 387 /// <summary> 388 /// 是否是日期 389 /// </summary> 390 /// <param name="date"></param> 391 /// <returns></returns> 392 public static bool IsDate(this object date) 393 { 394 395 //如果為空,認為驗證合格 396 if (IsNullOrEmpty(date)) 397 { 398 return false; 399 } 400 string strdate = date.ToString(); 401 try 402 { 403 //用轉換測試是否為規則的日期字元 404 date = Convert.ToDateTime(date).ToString("d"); 405 return true; 406 } 407 catch 408 { 409 //如果日期字串中存在非數字,則返回false 410 if (!IsInt(strdate)) 411 { 412 return false; 413 } 414 415 #region 對純數字進行解析 416 //對8位純數字進行解析 417 if (strdate.Length == 8) 418 { 419 //獲取年月日 420 string year = strdate.Substring(0, 4); 421 string month = strdate.Substring(4, 2); 422 string day = strdate.Substring(6, 2); 423 424 //驗證合法性 425 if (Convert.ToInt32(year) < 1900 || Convert.ToInt32(year) > 2100) 426 { 427 return false; 428 } 429 if (Convert.ToInt32(month) > 12 || Convert.ToInt32(day) > 31) 430 { 431 return false; 432 } 433 434 //拼接日期 435