StringExtenions.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using DbCommon.Enties.DbModels;
  2. using ProjectManagementSystem.Common.Config;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ProjectManagementSystem.Common.Extenions
  10. {
  11. public static class StringExtenions
  12. {
  13. public static T[] ToValueArray<T>(this string str)
  14. {
  15. return string.IsNullOrEmpty(str) ? new T[0] :
  16. str.Trim().Split(new char[] { ';', ',', '|' }, StringSplitOptions.RemoveEmptyEntries)
  17. .Select(s =>
  18. {
  19. return s.ToValue<T>();
  20. })
  21. .ToArray();
  22. }
  23. public static T[] ToValueArray<T>(this string str, params char[] separator)
  24. {
  25. return string.IsNullOrEmpty(str) ? new T[0] :
  26. str.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries)
  27. .Select(s =>
  28. {
  29. return s.ToValue<T>();
  30. })
  31. .ToArray();
  32. }
  33. public static T ToValue<T>(this string str)
  34. {
  35. if (string.IsNullOrEmpty(str)) return default(T);
  36. try
  37. {
  38. var type = typeof(T);
  39. if (type.Name == "Boolean")
  40. {
  41. var value = str == "1" || str == "True" || str == "true" ? true : false;
  42. return (T)Convert.ChangeType(value, type);
  43. }
  44. else
  45. {
  46. return (T)Convert.ChangeType(str, type);
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. Logger.CLog.Instance.SystemLog.WriteException(str, ex);
  52. throw ex;
  53. }
  54. }
  55. public static T ToValue<T>(this object obj)
  56. {
  57. if (obj == null) return default(T);
  58. return (T)Convert.ChangeType(obj, typeof(T));
  59. }
  60. public static string ToChineseString(this bool value)
  61. {
  62. return value ? "成功" : "失败";
  63. }
  64. public static string ToShortPosString(this string str)
  65. {
  66. var posArr = str.ToValueArray<string>();
  67. if (posArr == null) return null;
  68. List<string> shortPosList = new List<string>();
  69. for (int i = 0; i < posArr.Length; i++)
  70. {
  71. if (i > 0)
  72. {
  73. if (posArr[i] != posArr[i - 1])
  74. {
  75. shortPosList.Add(posArr[i]);
  76. }
  77. }
  78. else
  79. {
  80. shortPosList.Add(posArr[i]);
  81. }
  82. }
  83. return string.Join(",", shortPosList);
  84. }
  85. public static LocationStatus ToLocationStatus(this string status)
  86. {
  87. if (string.IsNullOrEmpty(status)) return LocationStatus.None;
  88. int tempValue;
  89. if (int.TryParse(status, out tempValue))
  90. {
  91. return (LocationStatus)tempValue;
  92. }
  93. return LocationStatus.None;
  94. }
  95. public static string ToStatusString(this LocationStatus status)
  96. {
  97. string statusStr = null;
  98. switch (status)
  99. {
  100. case LocationStatus.None:
  101. statusStr = "未知";
  102. break;
  103. case LocationStatus.Filled:
  104. statusStr = "满";
  105. break;
  106. case LocationStatus.Empty:
  107. statusStr = "空";
  108. break;
  109. case LocationStatus.Locked:
  110. statusStr = "锁定";
  111. break;
  112. case LocationStatus.LineCall:
  113. statusStr = "叫料";
  114. break;
  115. case LocationStatus.Exception:
  116. statusStr = "异常";
  117. break;
  118. default:
  119. statusStr = "未知";
  120. break;
  121. }
  122. return statusStr;
  123. }
  124. public static Color ToColor(this LocationStatus status)
  125. {
  126. return Function.ColorSetting.Instance.GetColor(status);
  127. }
  128. public static string ToHexString(this byte[] bytes)
  129. {
  130. return string.Join(" ", bytes.Select(d => d.ToString("X2")));
  131. }
  132. public static string GetLocationMember(this string location, string member)
  133. {
  134. return ExcelConfig.Instance.GetLocationMemberCache(location, member);
  135. }
  136. public static bool IsChineseText(this string addrDesc)
  137. {
  138. return System.Text.RegularExpressions.Regex.IsMatch(addrDesc, @"[\u4e00-\u9fa5]");
  139. }
  140. public static string GetPlcTaskAddr(this string location, string addrDesc)
  141. {
  142. if (!addrDesc.IsChineseText())
  143. {
  144. return addrDesc;
  145. }
  146. return location.GetLocationMember(addrDesc);
  147. }
  148. public static bool GetBitVaule(this ushort vaule, int bit)
  149. {
  150. return (vaule & (0x01 << bit)) > 0;
  151. }
  152. public static bool GetBitVaule(this byte vaule, int bit)
  153. {
  154. return (vaule & (0x01 << bit)) > 0;
  155. }
  156. public static T GetArrayValue<T>(this string str, int index)
  157. {
  158. if (string.IsNullOrEmpty(str)) return default(T);
  159. var array = str.ToValueArray<T>();
  160. if (array.Length == 0) return default(T);
  161. return array.Length > index ? array[index] : array[array.Length - 1];
  162. }
  163. }
  164. }