StringExtensions.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Globalization;
  2. namespace AipGateway.API.Domain.Common.Extensions
  3. {
  4. public static class StringExtensions
  5. {
  6. public static int GenerateOrderNumber()
  7. {
  8. var init = DateTime.UtcNow.Ticks.ToString();
  9. var orderNumber = Convert.ToInt32(init.Remove(init.Length - 10)) + new Random().Next();
  10. return orderNumber < 0 ? orderNumber * -1 : orderNumber;
  11. }
  12. public static List<string> ToStringList(this string thisString, char seperationChar = ',')
  13. {
  14. if (string.IsNullOrEmpty(thisString))
  15. {
  16. return new List<string>();
  17. }
  18. return thisString.Split(seperationChar).Distinct().ToList();
  19. }
  20. public static string ToCommaSeperatedString(this List<string> theseStrings)
  21. {
  22. return string.Join(", ", theseStrings);
  23. }
  24. public static string ToCommaSeperatedString(this List<int> theseIntegers)
  25. {
  26. return string.Join(", ", theseIntegers);
  27. }
  28. public static List<int> ToIntList(this string thisString, char seperationChar = ',')
  29. {
  30. if (string.IsNullOrEmpty(thisString))
  31. {
  32. return new List<int>();
  33. }
  34. var intList = new List<int>();
  35. foreach (var item in thisString.Split(seperationChar).Distinct().ToList())
  36. {
  37. intList.Add(Convert.ToInt32(item));
  38. }
  39. return intList;
  40. }
  41. public static DateTime ToDateTime(this string HexaDecimalTimeStamp)
  42. {
  43. var timestamp = HexaDecimalTimeStamp.ToInt();
  44. var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  45. return dateTime.AddSeconds(timestamp).ToLocalTime();
  46. }
  47. public static TimeSpan ToTimeSpan(this string HexaDecimalTimeStamp)
  48. {
  49. var timestamp = HexaDecimalTimeStamp.ToInt();
  50. return new TimeSpan(timestamp);
  51. }
  52. public static int ToInt(this string HexaDecimalTimeStamp)
  53. {
  54. return int.Parse(HexaDecimalTimeStamp, NumberStyles.HexNumber);
  55. }
  56. public static string ToLowerCase(this string QuestionItemTitle)
  57. {
  58. if (!string.IsNullOrEmpty(QuestionItemTitle))
  59. {
  60. QuestionItemTitle = QuestionItemTitle.Replace(" ", "");
  61. if (int.TryParse(QuestionItemTitle, out int intNumber))
  62. {
  63. return intNumber.ToString();
  64. }
  65. else if (decimal.TryParse(QuestionItemTitle, out decimal decimalNumber))
  66. {
  67. return decimalNumber.ToString();
  68. }
  69. else if (double.TryParse(QuestionItemTitle, out double doubleNumber))
  70. {
  71. return doubleNumber.ToString();
  72. }
  73. else
  74. {
  75. return QuestionItemTitle.ToLower();
  76. }
  77. }
  78. return string.Empty;
  79. }
  80. }
  81. }