平安校园
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

705 行
22 KiB

  1. using System.Collections;
  2. using System.Text.RegularExpressions;
  3. namespace SafeCampus.Core.Extension
  4. {
  5. public static partial class Extensions
  6. {
  7. #region 转换为long
  8. /// <summary>
  9. /// 将object转换为long,若转换失败,则返回0。不抛出异常。
  10. /// </summary>
  11. /// <param name="obj"></param>
  12. /// <returns></returns>
  13. public static long ParseToLong(this object obj)
  14. {
  15. try
  16. {
  17. if (obj == null)
  18. return 0L;
  19. return long.Parse(obj.ToString());
  20. }
  21. catch
  22. {
  23. return 0L;
  24. }
  25. }
  26. /// <summary>
  27. /// 将object转换为long,若转换失败,则返回指定值。不抛出异常。
  28. /// </summary>
  29. /// <param name="str"></param>
  30. /// <param name="defaultValue"></param>
  31. /// <returns></returns>
  32. public static long ParseToLong(this string str, long defaultValue)
  33. {
  34. try
  35. {
  36. return long.Parse(str);
  37. }
  38. catch
  39. {
  40. return defaultValue;
  41. }
  42. }
  43. #endregion
  44. #region 转换为int
  45. /// <summary>
  46. /// 将object转换为int,若转换失败,则返回0。不抛出异常。
  47. /// </summary>
  48. /// <param name="str"></param>
  49. /// <returns></returns>
  50. public static int ParseToInt(this object str)
  51. {
  52. try
  53. {
  54. return Convert.ToInt32(str);
  55. }
  56. catch
  57. {
  58. return 0;
  59. }
  60. }
  61. /// <summary>
  62. /// 将object转换为int,若转换失败,则返回指定值。不抛出异常。
  63. /// null返回默认值
  64. /// </summary>
  65. /// <param name="str"></param>
  66. /// <param name="defaultValue"></param>
  67. /// <returns></returns>
  68. public static int ParseToInt(this object str, int defaultValue)
  69. {
  70. if (str == null)
  71. {
  72. return defaultValue;
  73. }
  74. try
  75. {
  76. return Convert.ToInt32(str);
  77. }
  78. catch
  79. {
  80. return defaultValue;
  81. }
  82. }
  83. #endregion
  84. #region 转换为short
  85. /// <summary>
  86. /// 将object转换为short,若转换失败,则返回0。不抛出异常。
  87. /// </summary>
  88. /// <param name="obj"></param>
  89. /// <returns></returns>
  90. public static short ParseToShort(this object obj)
  91. {
  92. try
  93. {
  94. return short.Parse(obj.ToString());
  95. }
  96. catch
  97. {
  98. return 0;
  99. }
  100. }
  101. /// <summary>
  102. /// 将object转换为short,若转换失败,则返回指定值。不抛出异常。
  103. /// </summary>
  104. /// <param name="str"></param>
  105. /// <param name="defaultValue"></param>
  106. /// <returns></returns>
  107. public static short ParseToShort(this object str, short defaultValue)
  108. {
  109. try
  110. {
  111. return short.Parse(str.ToString());
  112. }
  113. catch
  114. {
  115. return defaultValue;
  116. }
  117. }
  118. #endregion
  119. #region 转换为demical
  120. /// <summary>
  121. /// 将object转换为demical,若转换失败,则返回指定值。不抛出异常。
  122. /// </summary>
  123. /// <param name="str"></param>
  124. /// <param name="defaultValue"></param>
  125. /// <returns></returns>
  126. public static decimal ParseToDecimal(this object str, decimal defaultValue)
  127. {
  128. try
  129. {
  130. return decimal.Parse(str.ToString());
  131. }
  132. catch
  133. {
  134. return defaultValue;
  135. }
  136. }
  137. /// <summary>
  138. /// 将object转换为demical,若转换失败,则返回0。不抛出异常。
  139. /// </summary>
  140. /// <param name="str"></param>
  141. /// <returns></returns>
  142. public static decimal ParseToDecimal(this object str)
  143. {
  144. try
  145. {
  146. return decimal.Parse(str.ToString());
  147. }
  148. catch
  149. {
  150. return 0;
  151. }
  152. }
  153. #endregion
  154. #region 转化为bool
  155. /// <summary>
  156. /// 将object转换为bool,若转换失败,则返回false。不抛出异常。
  157. /// </summary>
  158. /// <param name="str"></param>
  159. /// <returns></returns>
  160. public static bool ParseToBool(this object str)
  161. {
  162. try
  163. {
  164. return bool.Parse(str.ToString());
  165. }
  166. catch
  167. {
  168. return false;
  169. }
  170. }
  171. /// <summary>
  172. /// 将object转换为bool,若转换失败,则返回指定值。不抛出异常。
  173. /// </summary>
  174. /// <param name="str"></param>
  175. /// <param name="result"></param>
  176. /// <returns></returns>
  177. public static bool ParseToBool(this object str, bool result)
  178. {
  179. try
  180. {
  181. return bool.Parse(str.ToString());
  182. }
  183. catch
  184. {
  185. return result;
  186. }
  187. }
  188. #endregion
  189. #region 转换为float
  190. /// <summary>
  191. /// 将object转换为float,若转换失败,则返回0。不抛出异常。
  192. /// </summary>
  193. /// <param name="str"></param>
  194. /// <returns></returns>
  195. public static float ParseToFloat(this object str)
  196. {
  197. try
  198. {
  199. return float.Parse(str.ToString());
  200. }
  201. catch
  202. {
  203. return 0;
  204. }
  205. }
  206. /// <summary>
  207. /// 将object转换为float,若转换失败,则返回指定值。不抛出异常。
  208. /// </summary>
  209. /// <param name="str"></param>
  210. /// <param name="result"></param>
  211. /// <returns></returns>
  212. public static float ParseToFloat(this object str, float result)
  213. {
  214. try
  215. {
  216. return float.Parse(str.ToString());
  217. }
  218. catch
  219. {
  220. return result;
  221. }
  222. }
  223. #endregion
  224. #region 转换为Guid
  225. /// <summary>
  226. /// 将string转换为Guid,若转换失败,则返回Guid.Empty。不抛出异常。
  227. /// </summary>
  228. /// <param name="str"></param>
  229. /// <returns></returns>
  230. public static Guid ParseToGuid(this string str)
  231. {
  232. try
  233. {
  234. return new Guid(str);
  235. }
  236. catch
  237. {
  238. return Guid.Empty;
  239. }
  240. }
  241. #endregion
  242. #region 转换为DateTime
  243. /// <summary>
  244. /// 将string转换为DateTime,若转换失败,则返回日期最小值。不抛出异常。
  245. /// </summary>
  246. /// <param name="str"></param>
  247. /// <returns></returns>
  248. public static DateTime ParseToDateTime(this string str)
  249. {
  250. try
  251. {
  252. if (string.IsNullOrWhiteSpace(str))
  253. {
  254. return DateTime.MinValue;
  255. }
  256. if (str.Contains("-") || str.Contains("/"))
  257. {
  258. return DateTime.Parse(str);
  259. }
  260. else
  261. {
  262. int length = str.Length;
  263. switch (length)
  264. {
  265. case 4:
  266. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  267. case 6:
  268. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  269. case 8:
  270. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  271. case 10:
  272. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  273. case 12:
  274. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  275. case 14:
  276. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  277. default:
  278. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  279. }
  280. }
  281. }
  282. catch
  283. {
  284. return DateTime.MinValue;
  285. }
  286. }
  287. /// <summary>
  288. /// 将string转换为DateTime,若转换失败,则返回默认值。
  289. /// </summary>
  290. /// <param name="str"></param>
  291. /// <param name="defaultValue"></param>
  292. /// <returns></returns>
  293. public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
  294. {
  295. try
  296. {
  297. if (string.IsNullOrWhiteSpace(str))
  298. {
  299. return defaultValue.GetValueOrDefault();
  300. }
  301. if (str.Contains("-") || str.Contains("/"))
  302. {
  303. return DateTime.Parse(str);
  304. }
  305. else
  306. {
  307. int length = str.Length;
  308. switch (length)
  309. {
  310. case 4:
  311. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  312. case 6:
  313. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  314. case 8:
  315. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  316. case 10:
  317. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  318. case 12:
  319. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  320. case 14:
  321. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  322. default:
  323. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  324. }
  325. }
  326. }
  327. catch
  328. {
  329. return defaultValue.GetValueOrDefault();
  330. }
  331. }
  332. #endregion
  333. #region 转换为string
  334. /// <summary>
  335. /// 将object转换为string,若转换失败,则返回""。不抛出异常。
  336. /// </summary>
  337. /// <param name="obj"></param>
  338. /// <returns></returns>
  339. public static string ParseToString(this object obj)
  340. {
  341. try
  342. {
  343. if (obj == null)
  344. {
  345. return string.Empty;
  346. }
  347. else
  348. {
  349. return obj.ToString();
  350. }
  351. }
  352. catch
  353. {
  354. return string.Empty;
  355. }
  356. }
  357. public static string ParseToStrings<T>(this object obj)
  358. {
  359. try
  360. {
  361. if (obj is IEnumerable<T> list)
  362. {
  363. return string.Join(",", list);
  364. }
  365. else
  366. {
  367. return obj.ToString();
  368. }
  369. }
  370. catch
  371. {
  372. return string.Empty;
  373. }
  374. }
  375. #endregion
  376. #region 转换为double
  377. /// <summary>
  378. /// 将object转换为double,若转换失败,则返回0。不抛出异常。
  379. /// </summary>
  380. /// <param name="obj"></param>
  381. /// <returns></returns>
  382. public static double ParseToDouble(this object obj)
  383. {
  384. try
  385. {
  386. return double.Parse(obj.ToString());
  387. }
  388. catch
  389. {
  390. return 0;
  391. }
  392. }
  393. /// <summary>
  394. /// 将object转换为double,若转换失败,则返回指定值。不抛出异常。
  395. /// </summary>
  396. /// <param name="str"></param>
  397. /// <param name="defaultValue"></param>
  398. /// <returns></returns>
  399. public static double ParseToDouble(this object str, double defaultValue)
  400. {
  401. try
  402. {
  403. return double.Parse(str.ToString());
  404. }
  405. catch
  406. {
  407. return defaultValue;
  408. }
  409. }
  410. #endregion
  411. #region 强制转换类型
  412. /// <summary>
  413. /// 强制转换类型
  414. /// </summary>
  415. /// <typeparam name="TResult"></typeparam>
  416. /// <param name="source"></param>
  417. /// <returns></returns>
  418. public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source)
  419. {
  420. foreach (object item in source)
  421. {
  422. yield return (TResult)Convert.ChangeType(item, typeof(TResult));
  423. }
  424. }
  425. #endregion
  426. #region 阿拉伯数字转换为中文数字(0-99999)
  427. public static string NumberToChinese(int num)
  428. {
  429. string strNum = num.ToString();
  430. string result = "";
  431. if (strNum.Length == 5)
  432. {//万
  433. result += OneBitNumberToChinese(strNum.Substring(0, 1)) + "万";
  434. strNum = strNum.Substring(1);
  435. }
  436. if (strNum.Length == 4)
  437. {//千
  438. string secondBitNumber = strNum.Substring(0, 1);
  439. if (secondBitNumber == "0") result += "零";
  440. else result += OneBitNumberToChinese(secondBitNumber) + "千";
  441. strNum = strNum.Substring(1);
  442. }
  443. if (strNum.Length == 3)
  444. {//百
  445. string hundredBitNumber = strNum.Substring(0, 1);
  446. if (hundredBitNumber == "0" && result.Substring(result.Length - 1) != "零") result += "零";
  447. else result += OneBitNumberToChinese(hundredBitNumber) + "百";
  448. strNum = strNum.Substring(1);
  449. }
  450. if (strNum.Length == 2)
  451. {//十
  452. string hundredBitNumber = strNum.Substring(0, 1);
  453. if (hundredBitNumber == "0" && result.Substring(result.Length - 1) != "零") result += "零";
  454. else if (hundredBitNumber == "1" && string.IsNullOrEmpty(result)) result += "十";//10->十
  455. else result += OneBitNumberToChinese(hundredBitNumber) + "十";
  456. strNum = strNum.Substring(1);
  457. }
  458. if (strNum.Length == 1)
  459. {//个
  460. if (strNum == "0") result += "零";
  461. else result += OneBitNumberToChinese(strNum);
  462. }
  463. //100->一百零零
  464. if (!string.IsNullOrEmpty(result) && result != "零") result = result.TrimEnd('零');
  465. return result;
  466. }
  467. //数字1-9转换为中文数字
  468. private static string OneBitNumberToChinese(string num)
  469. {
  470. string numStr = "123456789";
  471. string chineseStr = "一二三四五六七八九";
  472. string result = "";
  473. int numIndex = numStr.IndexOf(num);
  474. if (numIndex > -1)
  475. {
  476. result = chineseStr.Substring(numIndex, 1);
  477. }
  478. return result;
  479. }
  480. #endregion
  481. #region 日期转大写
  482. /// <summary>
  483. /// 年月日转换为大写
  484. /// </summary>
  485. /// <param name="date"></param>
  486. /// <returns></returns>
  487. public static string YMDToUpper(DateTime date)
  488. {
  489. int year = date.Year;
  490. int month = date.Month;
  491. int day = date.Day;
  492. return numtoUpper(year) + "年" + monthtoUpper(month) + "月" + daytoUpper(day) + "日";
  493. }
  494. /// <summary>
  495. /// 年月转换为大写
  496. /// </summary>
  497. /// <param name="date"></param>
  498. /// <returns></returns>
  499. public static string YMToUpper(DateTime date)
  500. {
  501. int year = date.Year;
  502. int month = date.Month;
  503. return numtoUpper(year) + "年" + monthtoUpper(month) + "月";
  504. }
  505. //把数字转换为大写
  506. private static string numtoUpper(int num)
  507. {
  508. String str = num.ToString();
  509. string rstr = "";
  510. int n;
  511. for (int i = 0; i < str.Length; i++)
  512. {
  513. n = Convert.ToInt16(str[i].ToString());//char转数字,转换为字符串,再转数字
  514. switch (n)
  515. {
  516. case 0:
  517. rstr = rstr + "〇";
  518. break;
  519. case 1:
  520. rstr = rstr + "一";
  521. break;
  522. case 2:
  523. rstr = rstr + "二";
  524. break;
  525. case 3:
  526. rstr = rstr + "三";
  527. break;
  528. case 4:
  529. rstr = rstr + "四";
  530. break;
  531. case 5:
  532. rstr = rstr + "五";
  533. break;
  534. case 6:
  535. rstr = rstr + "六";
  536. break;
  537. case 7:
  538. rstr = rstr + "七";
  539. break;
  540. case 8:
  541. rstr = rstr + "八";
  542. break;
  543. default:
  544. rstr = rstr + "九";
  545. break;
  546. }
  547. }
  548. return rstr;
  549. }
  550. //月转化为大写
  551. private static string monthtoUpper(int month)
  552. {
  553. if (month < 10)
  554. {
  555. return numtoUpper(month);
  556. }
  557. else if (month == 10)
  558. {
  559. return "十";
  560. }
  561. else
  562. {
  563. return "十" + numtoUpper(month - 10);
  564. }
  565. }
  566. //日转化为大写
  567. private static string daytoUpper(int day)
  568. {
  569. if (day < 20)
  570. {
  571. return monthtoUpper(day);
  572. }
  573. else
  574. {
  575. String str = day.ToString();
  576. if (str[1] == '0')
  577. {
  578. return numtoUpper(Convert.ToInt16(str[0].ToString())) + "十";
  579. }
  580. else
  581. {
  582. return numtoUpper(Convert.ToInt16(str[0].ToString())) + "十" + numtoUpper(Convert.ToInt16(str[1].ToString()));
  583. }
  584. }
  585. }
  586. #endregion
  587. #region 时间戳转日期
  588. /// <summary>
  589. /// 时间戳转日期
  590. /// </summary>
  591. /// <param name="timeStamp"></param>
  592. /// <param name="isMinSeconds"></param>
  593. /// <returns></returns>
  594. public static DateTime StampToDatetime(this long timeStamp, bool isMinSeconds = false)
  595. {
  596. var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//当地时区
  597. //返回转换后的日期
  598. if (isMinSeconds)
  599. return startTime.AddMilliseconds(timeStamp);
  600. else
  601. return startTime.AddSeconds(timeStamp);
  602. }
  603. /// <summary>
  604. /// 时间戳转日期
  605. /// </summary>
  606. /// <param name="timeStamp"></param>
  607. /// <param name="isMinSeconds"></param>
  608. /// <returns></returns>
  609. public static DateTime StampToDatetime(this string timeStamp, bool isMinSeconds = false)
  610. {
  611. var timeStampLong = long.Parse(timeStamp);
  612. var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//当地时区
  613. //返回转换后的日期
  614. if (isMinSeconds)
  615. return startTime.AddMilliseconds(timeStampLong);
  616. else
  617. return startTime.AddSeconds(timeStampLong);
  618. }
  619. #endregion
  620. #region 字符串正则替换
  621. /// <summary>
  622. /// 字符串正则替换
  623. /// </summary>
  624. /// <param name="targetStr"></param>
  625. /// <param name="patternStr"></param>
  626. /// <param name="replaceStr"></param>
  627. /// <param name="isRecursion"></param>
  628. /// <returns></returns>
  629. public static string ReplaceMatchStr(this string targetStr, string patternStr, string replaceStr,bool isRecursion = true)
  630. {
  631. Regex regex= new Regex(patternStr);
  632. Match match= regex.Match(targetStr);
  633. return ReplaceMatchingStr(targetStr,match,replaceStr,isRecursion);
  634. }
  635. private static string ReplaceMatchingStr(string targetStr, Match match, string replaceStr, bool isRecursion)
  636. {
  637. //是否匹配成功
  638. if (match.Success)
  639. {
  640. //处理字符串
  641. targetStr = ReplaceStr(targetStr, match, replaceStr);
  642. //是否递归匹配
  643. if (isRecursion)
  644. targetStr = ReplaceMatchingStr(targetStr, match.NextMatch(), replaceStr, true);
  645. }
  646. return targetStr;
  647. }
  648. private static string ReplaceStr(string targetStr, Match match, string replaceStr)
  649. {
  650. //替换字符
  651. string newStr = targetStr.Replace(match.ToString(), replaceStr);
  652. return newStr;
  653. }
  654. public static string RedactString(this string input, int start, int length, char replaceChar)
  655. {
  656. if (input.Length < start + length)
  657. {
  658. return input;
  659. }
  660. string prefix = input.Substring(0, start);
  661. string suffix = input.Substring(start + length);
  662. string replacement = new string(replaceChar, length);
  663. return prefix + replacement + suffix;
  664. }
  665. #endregion
  666. }
  667. }