StringTool.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.3.29 - 2017.8.3
  5. // desc: 字符串工具类
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using Y.Utils.DataUtils.Collections;
  12. namespace Y.Utils.DataUtils.StringUtils
  13. {
  14. public sealed class StringTool
  15. {
  16. /// <summary>
  17. /// 判断字符串为null或为空格
  18. /// </summary>
  19. /// <param name="str"></param>
  20. /// <returns></returns>
  21. public static bool IsNullOrWhiteSpace(string str)
  22. {
  23. if (str == null)
  24. return true;
  25. if (str.Trim().Length == 0)
  26. return true;
  27. return false;
  28. }
  29. /// <summary>
  30. /// 分割字符串
  31. /// </summary>
  32. /// <param name="str"></param>
  33. /// <param name="separator"></param>
  34. /// <param name="result"></param>
  35. /// <returns></returns>
  36. public static int Split(string str, char separator, out string[] result)
  37. {
  38. if (!string.IsNullOrWhiteSpace(str))
  39. {
  40. string[] list = str.Split(separator);
  41. if (ListTool.HasElements(list))
  42. {
  43. result = list;
  44. return result.Length;
  45. }
  46. }
  47. result = null;
  48. return 0;
  49. }
  50. /// <summary>
  51. /// 字符串中字符出现次数
  52. /// </summary>
  53. /// <param name="s"></param>
  54. /// <param name="sub"></param>
  55. /// <returns></returns>
  56. public static int SubStringCount(string s, string sub)
  57. {
  58. if (s.Contains(sub))
  59. {
  60. string sReplaced = s.Replace(sub, "");
  61. return (s.Length - sReplaced.Length) / sub.Length;
  62. }
  63. return 0;
  64. }
  65. }
  66. }