StringExtenions.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace DbCommon.Extenions
  7. {
  8. public static class StringExtenions
  9. {
  10. public static int[] ToIntArray(this string str)
  11. {
  12. return string.IsNullOrEmpty(str) ? new int[0] :
  13. str.Trim().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
  14. .Select(s =>
  15. {
  16. int mark = 0;
  17. return int.TryParse(s, out mark) ? mark : 0;
  18. })
  19. .ToArray();
  20. }
  21. public static string[] ToStringArray(this string str)
  22. {
  23. return string.IsNullOrEmpty(str) ? new string[0] :
  24. str.Trim().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
  25. .ToArray();
  26. }
  27. public static string[] ToPosArray(this string str)
  28. {
  29. return Function.Tools.StringToPosArray(str);
  30. }
  31. public static string ToPosString(this string[] strArray)
  32. {
  33. return Function.Tools.PosArrayToString(strArray);
  34. }
  35. public static string ToChineseString(this bool value)
  36. {
  37. return value ? "成功" : "失败";
  38. }
  39. public static bool[] ToBoolArray(this string str)
  40. {
  41. return string.IsNullOrEmpty(str) ? new bool[0] :
  42. str.Trim().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
  43. .Select(s =>
  44. {
  45. return s == "1" || s == "True" || s == "true" ? true : false;
  46. })
  47. .ToArray();
  48. }
  49. public static int ToInt(this string str)
  50. {
  51. return string.IsNullOrEmpty(str) ? 0
  52. : (int.TryParse(str, out int mark) ? mark : 0);
  53. }
  54. public static double ToDouble(this string str)
  55. {
  56. return string.IsNullOrEmpty(str) ? 0.0
  57. : (double.TryParse(str, out double mark) ? mark : 0.0);
  58. }
  59. }
  60. }