Extensions.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. namespace System
  10. {
  11. /// <summary>
  12. /// DateTime扩展
  13. /// </summary>
  14. public static partial class DateTimeExtensions
  15. {
  16. //public static DateTime ToUUDateTime(this DateTime dateTime)
  17. //{
  18. // return new DateTime(dateTime.Ticks);
  19. //}
  20. //public static DateTime ToUUDate(this DateTime dateTime)
  21. //{
  22. // return new DateTime(dateTime.Date.Ticks);
  23. //}
  24. public static string ToString_yyyyMMddHHmmssffff(this DateTime dateTime)
  25. {
  26. return dateTime.ToString("yyyy-MM-dd HH:mm:ss.ffff");
  27. }
  28. public static string ToString_yyyyMMddHHmmss(this DateTime dateTime)
  29. {
  30. return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
  31. }
  32. public static string ToString_yyyyMMddHHmm(this DateTime dateTime)
  33. {
  34. return dateTime.ToString("yyyy-MM-dd HH:mm");
  35. }
  36. public static string ToString_yyyyMMddHH(this DateTime dateTime)
  37. {
  38. return dateTime.ToString("yyyy-MM-dd HH");
  39. }
  40. public static string ToString_yyyyMMdd(this DateTime dateTime)
  41. {
  42. return dateTime.ToString("yyyy-MM-dd");
  43. }
  44. public static string To_yyyyMMdd(this DateTime dateTime)
  45. {
  46. return dateTime.ToString("yyyyMMdd");
  47. }
  48. public static string ToString_yyyyMM(this DateTime dateTime)
  49. {
  50. return dateTime.ToString("yyyy-MM");
  51. }
  52. public static string ToString_HHmmssffff(this DateTime dateTime)
  53. {
  54. return dateTime.ToString("HH:mm:ss.ffff");
  55. }
  56. public static string ToString_HHmmss(this DateTime dateTime)
  57. {
  58. return dateTime.ToString("HH:mm:ss");
  59. }
  60. public static string ToString_HHmm(this DateTime dateTime)
  61. {
  62. return dateTime.ToString("HH:mm");
  63. }
  64. /// <summary>
  65. /// 获取两个日期间隔的总月
  66. /// </summary>
  67. /// <param name="dateTime"></param>
  68. /// <param name="other"></param>
  69. /// <returns></returns>
  70. public static int MonthDifference(this DateTime dateTime, DateTime other)
  71. {
  72. return Math.Abs((dateTime.Year - other.Year - 1) * 12 + (12 - other.Month) + dateTime.Month);
  73. }
  74. /// <summary>
  75. /// 克转换为千克的字符串
  76. /// </summary>
  77. /// <param name="gram">克</param>
  78. /// <param name="separated">千位是否按逗号分开</param>
  79. /// <returns></returns>
  80. public static string KGFormat(this int gram, bool separated = false)
  81. {
  82. string format = "F3";
  83. if (separated) format = "N3";
  84. return (gram / 1000d).ToString(format);
  85. }
  86. /// <summary>
  87. /// 过滤非英文字符或非数字字符
  88. /// </summary>
  89. /// <param name="str"></param>
  90. /// <returns></returns>
  91. public static string FileterNonNumberAndEnChar(this string str)
  92. {
  93. if (string.IsNullOrWhiteSpace(str)) return string.Empty;
  94. return Regex.Replace(str, @"[^a-zA-Z0-9]", "");
  95. }
  96. }
  97. /// <summary>
  98. /// String类扩展
  99. /// </summary>
  100. /// <remarks>
  101. /// [2013-12-14] Develop by SunLiang.
  102. /// </remarks>
  103. public static partial class StringExtensions
  104. {
  105. public static bool IsNullOrEmpty(this string s)
  106. {
  107. return string.IsNullOrEmpty(s);
  108. }
  109. public static string Formater(this string format, params object[] args)
  110. {
  111. return string.Format(format, args);
  112. }
  113. public static string Formater(this string format, object arg0)
  114. {
  115. return string.Format(format, arg0);
  116. }
  117. public static string Formater(this string format, string arg0, object arg1)
  118. {
  119. return string.Format(format, arg0, arg1);
  120. }
  121. public static string Formater(this string format, object arg0, object arg1)
  122. {
  123. return string.Format(format, arg0, arg1);
  124. }
  125. public static string Formater(this string format, object arg0, object arg1, object arg2)
  126. {
  127. return string.Format(format, arg0, arg1, arg2);
  128. }
  129. public static string Formater(this string format, IFormatProvider provider, params object[] args)
  130. {
  131. return string.Format(provider, format, args);
  132. }
  133. public static bool IsMatch(this string s, string pattern)
  134. {
  135. if (s == null) return false;
  136. else return Regex.IsMatch(s, pattern);
  137. }
  138. public static string Match(this string s, string pattern)
  139. {
  140. if (s == null) return "";
  141. return Regex.Match(s, pattern).Value;
  142. }
  143. public static bool IsInt(this string s)
  144. {
  145. int i;
  146. return int.TryParse(s, out i);
  147. }
  148. public static int ToInt(this string s)
  149. {
  150. return int.Parse(s);
  151. }
  152. /// <summary>
  153. /// 转Int32位数字类型
  154. /// </summary>
  155. /// <param name="s">待转Int数据类型</param>
  156. /// <param name="defult">转失败则返回默认值,没有输入的默认为0</param>
  157. /// <returns>转成功后Int数值</returns>
  158. public static int ToInt(this string s, int defult = 0)
  159. {
  160. int value = defult;
  161. if (int.TryParse(s, out value)) //如果转成int成功则为int
  162. return value;
  163. else
  164. return value;
  165. }
  166. public static string ToCamel(this string s)
  167. {
  168. if (s.IsNullOrEmpty()) return s;
  169. return s[0].ToString().ToLower() + s.Substring(1);
  170. }
  171. public static string ToPascal(this string s)
  172. {
  173. if (s.IsNullOrEmpty()) return s;
  174. return s[0].ToString().ToUpper() + s.Substring(1);
  175. }
  176. /// <summary>
  177. /// 将字符串表示形式转换为它的等效 32 位有符号整数,如转换失败返回 0
  178. /// </summary>
  179. /// <param name="s"></param>
  180. /// <returns></returns>
  181. public static int TryConvertInt32(this string s)
  182. {
  183. int result = 0;
  184. int.TryParse(s, out result);
  185. return result;
  186. }
  187. public static bool TryConvertBoolean(this string s)
  188. {
  189. bool result = false;
  190. bool.TryParse(s, out result);
  191. return result;
  192. }
  193. /// <summary>
  194. /// 将字符串表示形式转换为它的等效Decimal类型,如转换失败返回 0
  195. /// </summary>
  196. /// <param name="s"></param>
  197. /// <returns></returns>
  198. public static decimal TryConvertDecimal(this string s)
  199. {
  200. decimal result = 0;
  201. decimal.TryParse(s, out result);
  202. return result;
  203. }
  204. /// <summary>
  205. /// 将字符串表示形式转换为它的等效Double类型,如转换失败返回 0
  206. /// </summary>
  207. /// <param name="s"></param>
  208. /// <returns></returns>
  209. public static double TryConvertDouble(this string s)
  210. {
  211. double result = 0;
  212. double.TryParse(s, out result);
  213. return result;
  214. }
  215. /// <summary>
  216. ///
  217. /// </summary>
  218. /// <param name="s"></param>
  219. /// <returns></returns>
  220. public static short TryConvertShort(this string s)
  221. {
  222. short result = 0;
  223. short.TryParse(s, out result);
  224. return result;
  225. }
  226. /// <summary>
  227. /// 将字符串按逗号分割成数组
  228. /// </summary>
  229. /// <typeparam name="T"></typeparam>
  230. /// <param name="s"></param>
  231. /// <returns></returns>
  232. public static T[] SplitToArray<T>(this string s) where T : IConvertible
  233. {
  234. if (s.IsNullOrEmpty()) return new T[0];
  235. string[] array = s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  236. List<T> ids = new List<T>();
  237. Type t = typeof(T);
  238. foreach (var item in array)
  239. {
  240. try
  241. {
  242. ids.Add((T)Convert.ChangeType(item, t));
  243. }
  244. catch { }
  245. }
  246. return ids.ToArray();
  247. }
  248. /// <summary>
  249. /// 将字符串按<see cref="seperator"/>分割成数组
  250. /// </summary>
  251. /// <typeparam name="T"></typeparam>
  252. /// <param name="s"></param>
  253. /// <param name="seperator">seperator</param>
  254. /// <returns></returns>
  255. public static T[] SplitToArray<T>(this string s, params char[] seperator) where T : IConvertible
  256. {
  257. if (s.IsNullOrEmpty()) return new T[0];
  258. string[] array = s.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  259. List<T> ids = new List<T>();
  260. Type t = typeof(T);
  261. foreach (var item in array)
  262. {
  263. ids.Add((T)Convert.ChangeType(item, t));
  264. }
  265. return ids.ToArray();
  266. }
  267. /// <summary>
  268. /// 将字符串按<see cref="seperator"/>分割成数组
  269. /// </summary>
  270. /// <typeparam name="T"></typeparam>
  271. /// <param name="s"></param>
  272. /// <param name="seperator">seperator</param>
  273. /// <returns></returns>
  274. public static T[] SplitToArray<T>(this string s, char seperator) where T : IConvertible
  275. {
  276. if (s.IsNullOrEmpty()) return new T[0];
  277. string[] array = s.Split(new[] { seperator }, StringSplitOptions.RemoveEmptyEntries);
  278. List<T> ids = new List<T>();
  279. Type t = typeof(T);
  280. foreach (var item in array)
  281. {
  282. ids.Add((T)Convert.ChangeType(item, t));
  283. }
  284. return ids.ToArray();
  285. }
  286. /// <summary>
  287. /// 将字符串按逗号分割成数组
  288. /// </summary>
  289. /// <typeparam name="T"></typeparam>
  290. /// <param name="s"></param>
  291. /// <returns></returns>
  292. public static List<T> SplitToList<T>(this string s) where T : IConvertible
  293. {
  294. if (s.IsNullOrEmpty()) return new List<T>();
  295. string[] array = s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  296. List<T> ids = new List<T>();
  297. Type t = typeof(T);
  298. foreach (var item in array)
  299. {
  300. ids.Add((T)Convert.ChangeType(item, t));
  301. }
  302. return ids;
  303. }
  304. public static string TrySubString(this string s, int maxLen)
  305. {
  306. if(string.IsNullOrWhiteSpace(s) || s.Length <= maxLen) return s;
  307. else
  308. {
  309. return s.Substring(0, maxLen);
  310. }
  311. }
  312. /// <summary>
  313. /// 将字符串按<see cref="seperator"/>分割成数组
  314. /// </summary>
  315. /// <typeparam name="T"></typeparam>
  316. /// <param name="s"></param>
  317. /// <param name="seperator">seperator</param>
  318. /// <returns></returns>
  319. public static List<T> SplitToList<T>(this string s, params char[] seperator) where T : IConvertible
  320. {
  321. if (s.IsNullOrEmpty()) return new List<T>();
  322. string[] array = s.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  323. List<T> ids = new List<T>();
  324. Type t = typeof(T);
  325. foreach (var item in array)
  326. {
  327. ids.Add((T)Convert.ChangeType(item, t));
  328. }
  329. return ids;
  330. }
  331. /// <summary>
  332. /// 将字符串按<see cref="seperator"/>分割成数组
  333. /// </summary>
  334. /// <typeparam name="T"></typeparam>
  335. /// <param name="s"></param>
  336. /// <param name="seperator">seperator</param>
  337. /// <returns></returns>
  338. public static List<T> SplitToList<T>(this string s, char seperator) where T : IConvertible
  339. {
  340. if (s.IsNullOrEmpty()) return new List<T>();
  341. string[] array = s.Split(new[] { seperator }, StringSplitOptions.RemoveEmptyEntries);
  342. List<T> ids = new List<T>();
  343. Type t = typeof(T);
  344. foreach (var item in array)
  345. {
  346. ids.Add((T)Convert.ChangeType(item, t));
  347. }
  348. return ids;
  349. }
  350. //public static string[] ToArray(string str, int maxLength)
  351. //{
  352. // string[] arr = str.Split(' ', '.');
  353. // int page = ((str.Length + maxLength) - 1) / maxLength;
  354. // string[] addressArr = new string[page];
  355. // int flag = 0;
  356. // int t = arr.Count();
  357. // for (int i = 0; i < page; i++)
  358. // {
  359. // string temp = "";
  360. // for (int j = 0; j < t; j++)
  361. // {
  362. // if ((temp += arr[flag] + " ").Length <= maxLength)
  363. // {
  364. // addressArr[i] += arr[flag] + " ";
  365. // flag++;
  366. // }
  367. // }
  368. // t = arr.Count() - flag;
  369. // }
  370. // return addressArr;
  371. //}
  372. }
  373. /// <summary>
  374. /// Type 拓展
  375. /// </summary>
  376. public static partial class TypeExtensions
  377. {
  378. /// <summary>
  379. /// 确定当前实例是否是继承或者实现某个Type
  380. /// </summary>
  381. /// <param name="type"></param>
  382. /// <returns></returns>
  383. public static bool IsInheritOfType(this Type self, Type type)
  384. {
  385. if (type.IsInterface)
  386. return self.GetInterface(type.Name) != null;
  387. if (type.IsEnum)
  388. return self.GetEnumUnderlyingType() == type;
  389. return self.IsSubclassOf(type);
  390. }
  391. }
  392. public static partial class EnumExtensions
  393. {
  394. /// <summary>
  395. /// 枚举信息
  396. /// </summary>
  397. public struct EnumInfo
  398. {
  399. /// <summary>
  400. /// 名称
  401. /// </summary>
  402. public string Name { get; set; }
  403. /// <summary>
  404. /// 值
  405. /// </summary>
  406. public object Value { get; set; }
  407. /// <summary>
  408. /// 备注
  409. /// </summary>
  410. public string Remark { get; set; }
  411. }
  412. //[Obsolete("该方法已经过时,请参考使用 GetName() 或者 GetText() 或者 GetValue() 方法.", false)]
  413. //public static string AsString(this Enum value)
  414. //{
  415. // FieldInfo fi = value.GetType().GetField(value.ToString());
  416. // DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  417. // if ((attributes != null) && (attributes.Length > 0))
  418. // return attributes[0].Description;
  419. // else
  420. // return value.ToString();
  421. //}
  422. /// <summary>
  423. /// 获取枚举值的名称
  424. /// </summary>
  425. /// <param name="value"></param>
  426. /// <returns></returns>
  427. public static string GetName(this Enum value)
  428. {
  429. return Enum.GetName(value.GetType(), value);
  430. }
  431. /// <summary>
  432. /// 获取该枚举的显示值(如果使用了DescriptionAttribute 标签则显示描述中的别名,否则使用 Enum 的名称。)
  433. /// </summary>
  434. /// <param name="value"></param>
  435. /// <returns></returns>
  436. public static string GetText(this Enum value)
  437. {
  438. FieldInfo fi = value.GetType().GetField(value.ToString());
  439. DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  440. if ((attributes != null) && (attributes.Length > 0))
  441. return attributes[0].Description;
  442. else
  443. return value.ToString();
  444. }
  445. /// <summary>
  446. /// 获取该枚举的 Description 标记值
  447. /// </summary>
  448. /// <param name="value"></param>
  449. /// <returns></returns>
  450. public static string GetDescription(this Enum value)
  451. {
  452. FieldInfo fi = value.GetType().GetField(value.ToString());
  453. if (fi == null) return string.Empty;
  454. DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  455. if ((attributes != null) && (attributes.Length > 0))
  456. return attributes[0].Description;
  457. else
  458. return null;
  459. }
  460. /// <summary>
  461. /// 获取枚举代表的值
  462. /// </summary>
  463. /// <param name="value"></param>
  464. /// <returns></returns>
  465. public static int GetValue(this Enum value)
  466. {
  467. return Convert.ToInt32(value);
  468. }
  469. public static T Parse<T>(this Enum enumThis, int value)
  470. {
  471. return (T)Enum.Parse(enumThis.GetType(), value.ToString());
  472. }
  473. public static T Parse<T>(this Enum enumThis, string value)
  474. {
  475. return (T)Enum.Parse(enumThis.GetType(), value);
  476. }
  477. /// <summary>
  478. /// 获取枚举信息(名称,值,备注)
  479. /// </summary>
  480. /// <param name="em"></param>
  481. /// <returns></returns>
  482. public static List<EnumInfo> GetEnumInfo(Type em)
  483. {
  484. List<EnumInfo> emInfos = new List<EnumInfo>();
  485. var fields = em.GetFields(BindingFlags.Static | BindingFlags.Public);
  486. foreach (var fi in fields)
  487. {
  488. EnumInfo emInfo = new EnumInfo();
  489. emInfo.Name = fi.Name;
  490. emInfo.Value = fi.GetRawConstantValue();
  491. var remarkAttr = fi.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();
  492. if (remarkAttr != null)
  493. {
  494. emInfo.Remark = ((DescriptionAttribute)remarkAttr).Description;
  495. }
  496. emInfos.Add(emInfo);
  497. }
  498. return emInfos;
  499. }
  500. ///// <summary>
  501. ///// 确定包含Flag标记的枚举是否包含指定值
  502. ///// </summary>
  503. //public static bool Has(this Enum source, Enum target)
  504. //{
  505. // var value = Convert.ToInt32(target);
  506. // return (Convert.ToInt32(source) & value) == value;
  507. //}
  508. }
  509. /// <summary>
  510. ///
  511. /// </summary>
  512. /// <typeparam name="T"></typeparam>
  513. public partial class BindComboxEnumType<T>
  514. {
  515. public string Name
  516. {
  517. get;
  518. set;
  519. }
  520. public string TypeValue
  521. {
  522. get;
  523. set;
  524. }
  525. private static readonly List<BindComboxEnumType<T>> bindTypes;
  526. static BindComboxEnumType()
  527. {
  528. bindTypes = new List<BindComboxEnumType<T>>();
  529. Type t = typeof(T);
  530. foreach (FieldInfo field in t.GetFields())
  531. {
  532. if (field.IsSpecialName == false)
  533. {
  534. BindComboxEnumType<T> bind = new BindComboxEnumType<T>();
  535. bind.Name = field.Name;
  536. bind.TypeValue = field.GetRawConstantValue().ToString();
  537. bindTypes.Add(bind);
  538. }
  539. }
  540. }
  541. public static List<BindComboxEnumType<T>> BindTypes
  542. {
  543. get
  544. {
  545. return bindTypes;
  546. }
  547. }
  548. }
  549. public static partial class ArrayExtensions
  550. {
  551. //public static void forEach(this Array array, Action<object> action)
  552. //{
  553. // foreach (var item in array)
  554. // {
  555. // action(item);
  556. // }
  557. //}
  558. //public static void forEach<T>(this IEnumerable<T> array, Action<T> action)
  559. //{
  560. // foreach (var item in array)
  561. // {
  562. // action(item);
  563. // }
  564. //}
  565. }
  566. public static partial class StringBuilderExtensions
  567. {
  568. public static void TrimStart(this StringBuilder builder, char c)
  569. {
  570. TrimStart(builder, new[] { c });
  571. }
  572. public static void TrimStart(this StringBuilder builder, params char[] c)
  573. {
  574. if (builder.Length == 0) return;
  575. int a = 0;
  576. while (c.Contains(builder[a]))
  577. {
  578. a++;
  579. }
  580. if (a > 0)
  581. {
  582. builder = builder.Remove(0, a > builder.Length ? builder.Length : a);
  583. }
  584. }
  585. public static void TrimEnd(this StringBuilder builder, char c)
  586. {
  587. TrimEnd(builder, new[] { c });
  588. }
  589. public static void TrimEnd(this StringBuilder builder, params char[] c)
  590. {
  591. if (builder.Length == 0) return;
  592. int a = builder.Length;
  593. while (c.Contains(builder[a]))
  594. {
  595. a--;
  596. }
  597. if (a < builder.Length)
  598. {
  599. builder = builder.Remove(a < 0 ? 0 : a, builder.Length - a);
  600. }
  601. }
  602. public static void Trim(this StringBuilder builder, char c)
  603. {
  604. Trim(builder, new[] { c });
  605. }
  606. public static void Trim(this StringBuilder builder, params char[] c)
  607. {
  608. TrimStart(builder, c);
  609. TrimEnd(builder, c);
  610. }
  611. public static void Concat(this StringBuilder builder, params StringBuilder[] c)
  612. {
  613. foreach (var item in c)
  614. {
  615. if (!Object.ReferenceEquals(item, c))
  616. builder.Append(c.ToString());
  617. }
  618. }
  619. }
  620. /// <summary>
  621. /// 将一个实体对象的值复制到另一个对象
  622. /// </summary>
  623. public static partial class ObjectExtensions
  624. {
  625. public static T Copy<T>(this object obj) where T : class
  626. {
  627. if (obj == null) throw new ArgumentNullException("参数不能为null");
  628. T b = Activator.CreateInstance<T>();
  629. var properties = b.GetType().GetProperties().Where(p => p.PropertyType.IsPublic && p.CanWrite).ToList();
  630. var t = obj.GetType().GetProperties();
  631. foreach (var item in t)
  632. {
  633. var pro = properties.FirstOrDefault(x => x.Name == item.Name && x.PropertyType == item.PropertyType);
  634. if (pro != null)
  635. {
  636. pro.SetValue(b, item.GetValue(obj, null), null);
  637. }
  638. }
  639. return b;
  640. }
  641. }
  642. /// <summary>
  643. /// 将一个集合对象的值复制到另一个对象
  644. /// </summary>
  645. public static partial class IListExtensions
  646. {
  647. public static IList<T> CopyList<T>(this IEnumerable obj) where T : class
  648. {
  649. if (obj == null) throw new ArgumentNullException("参数不能为null");
  650. Type tp = obj.GetType();
  651. if (!tp.IsGenericType) throw new ArgumentException("参数类型必须为泛型集合");
  652. ICollection collection = obj as ICollection;
  653. IList<T> list = new List<T>();
  654. foreach (var item in obj)
  655. {
  656. list.Add(item.Copy<T>());
  657. }
  658. return list;
  659. }
  660. }
  661. public static partial class IntExtensions
  662. {
  663. private const string X36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  664. public static string ConvertTo36(this int val)
  665. {
  666. string result = "";
  667. while (val >= 36)
  668. {
  669. result = X36[val % 36] + result;
  670. val /= 36;
  671. }
  672. if (val >= 0) result = X36[val] + result;
  673. return result;
  674. }
  675. public static bool In(this int val, IEnumerable<int> list)
  676. {
  677. if (!list.Contains(val))
  678. {
  679. return false;
  680. }
  681. return true;
  682. }
  683. public static string IntArrayFormat(this string src)
  684. {
  685. if (!string.IsNullOrWhiteSpace(src))
  686. {
  687. var arr = src.Split(",,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  688. List<string> ids = new List<string>();
  689. foreach (string i in arr)
  690. {
  691. if (int.TryParse(i, out int rs))
  692. ids.Add(i);
  693. }
  694. src = string.Join(",", ids.ToArray());
  695. return src;
  696. }
  697. else return null;
  698. }
  699. public static bool IsASCII(this string str)
  700. {
  701. return str.All(c => c < 128);
  702. }
  703. }
  704. }