StringTool.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2017.10.12 - 2018.5.17
  4. // desc: 字符串工具类
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.DataUtils.CollectionUtils;
  8. using System;
  9. using System.Text.RegularExpressions;
  10. namespace Azylee.Core.DataUtils.StringUtils
  11. {
  12. public sealed class StringTool
  13. {
  14. /// <summary>
  15. /// 判断字符串 非null、""、空格(Not NullOrWhiteSpace)
  16. /// </summary>
  17. /// <param name="s"></param>
  18. /// <returns></returns>
  19. public static bool Ok(string s)
  20. {
  21. return !string.IsNullOrWhiteSpace(s);
  22. }
  23. /// <summary>
  24. /// 判断字符串 非null、""(Not NullOrEmpty)
  25. /// </summary>
  26. /// <param name="s"></param>
  27. /// <returns></returns>
  28. public static bool Ok2(string s)
  29. {
  30. return !string.IsNullOrEmpty(s);
  31. }
  32. /// <summary>
  33. /// 判断字符串为null或为空格
  34. /// </summary>
  35. /// <param name="str"></param>
  36. /// <returns></returns>
  37. public static bool IsNullOrWhiteSpace(string str)
  38. {
  39. if (str == null)
  40. return true;
  41. if (str.Trim().Length == 0)
  42. return true;
  43. return false;
  44. }
  45. /// <summary>
  46. /// 查看字符串包含字符(不区分大小写)
  47. /// </summary>
  48. /// <param name="s"></param>
  49. /// <param name="word"></param>
  50. /// <returns></returns>
  51. public static bool Contains(string s, string word)
  52. {
  53. if (s.ToLower().Contains(word.ToLower()))
  54. return true;
  55. return false;
  56. }
  57. /// <summary>
  58. /// 分割字符串
  59. /// </summary>
  60. /// <param name="str"></param>
  61. /// <param name="separator"></param>
  62. /// <param name="result"></param>
  63. /// <returns></returns>
  64. public static int Split(string str, char separator, out string[] result)
  65. {
  66. if (!string.IsNullOrWhiteSpace(str))
  67. {
  68. string[] list = str.Split(separator);
  69. if (ListTool.HasElements(list))
  70. {
  71. result = list;
  72. return result.Length;
  73. }
  74. }
  75. result = null;
  76. return 0;
  77. }
  78. /// <summary>
  79. /// 字符串中字符出现次数
  80. /// </summary>
  81. /// <param name="s"></param>
  82. /// <param name="sub"></param>
  83. /// <returns></returns>
  84. public static int SubStringCount(string s, string sub)
  85. {
  86. if (s.Contains(sub))
  87. {
  88. string sReplaced = s.Replace(sub, "");
  89. return (s.Length - sReplaced.Length) / sub.Length;
  90. }
  91. return 0;
  92. }
  93. /// <summary>
  94. /// 根据通配符验证字符串
  95. /// </summary>
  96. /// <param name="s">字符串</param>
  97. /// <param name="pattern">通配符:%和_</param>
  98. /// <returns></returns>
  99. public static bool IsMatch(string s, string pattern)
  100. {
  101. try
  102. {
  103. //key = key.Replace("%", @"[\s\S]*").Replace("_", @"[\s\S]");
  104. pattern = pattern.Replace("%", ".*").Replace("_", ".");
  105. return Regex.IsMatch(s, pattern);
  106. }
  107. catch
  108. {
  109. return false;
  110. }
  111. }
  112. /// <summary>
  113. /// 使用指定字符替换字符串中换行符
  114. /// </summary>
  115. /// <param name="s"></param>
  116. /// <param name="sign"></param>
  117. /// <returns></returns>
  118. public static string ReplaceNewLine(string s, string sign = " , ")
  119. {
  120. try
  121. {
  122. return s.Replace("\r\n", sign).
  123. Replace("\n\r", sign).
  124. Replace(Environment.NewLine, sign);
  125. }
  126. catch { return s; }
  127. }
  128. }
  129. }