StringExtension.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace Extensions
  6. {
  7. public static class StringExtension
  8. {
  9. /// <summary>
  10. /// SQL条件拼接
  11. /// </summary>
  12. /// <param name="str"></param>
  13. /// <param name="condition"></param>
  14. /// <returns></returns>
  15. public static string If(this string str, bool condition)
  16. {
  17. return condition ? str : string.Empty;
  18. }
  19. /// <summary>
  20. /// 判断是否为空
  21. /// </summary>
  22. /// <param name="str"></param>
  23. /// <returns></returns>
  24. public static bool IfNotEmpty(this string str)
  25. {
  26. return !string.IsNullOrEmpty(str);
  27. }
  28. /// <summary>
  29. /// 注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//(");
  30. /// </summary>
  31. /// <param name="input"></param>
  32. /// <param name="oldValue"></param>
  33. /// <param name="newValue"></param>
  34. /// <returns></returns>
  35. public static string ReplaceFirst(this string input, string oldValue, string newValue)
  36. {
  37. Regex regEx = new Regex(oldValue, RegexOptions.Multiline);
  38. return regEx.Replace(input, newValue == null ? "" : newValue, 1);
  39. }
  40. /// <summary>
  41. /// 骆驼峰转下划线
  42. /// </summary>
  43. /// <param name="name"></param>
  44. /// <returns></returns>
  45. public static string ToSmallCamelCase(string name)
  46. {
  47. var stringBuilder = new StringBuilder();
  48. stringBuilder.Append(name.Substring(0, 1).ToLower());
  49. for (var i = 0; i < name.Length; i++)
  50. {
  51. if (i == 0)
  52. {
  53. stringBuilder.Append(name.Substring(0, 1).ToLower());
  54. }
  55. else
  56. {
  57. if (name[i] >= 'A' && name[i] <= 'Z')
  58. {
  59. stringBuilder.Append($"_{name.Substring(i, 1).ToLower()}");
  60. }
  61. else
  62. {
  63. stringBuilder.Append(name[i]);
  64. }
  65. }
  66. }
  67. return stringBuilder.ToString();
  68. }
  69. /// <summary>
  70. /// 下划线命名转驼峰命名
  71. /// </summary>
  72. /// <param name="underscore"></param>
  73. /// <returns></returns>
  74. public static string UnderScoreToCamelCase(this string underscore)
  75. {
  76. string[] ss = underscore.Split("_");
  77. if (ss.Length == 1)
  78. {
  79. return underscore;
  80. }
  81. StringBuilder sb = new();
  82. sb.Append(ss[0]);
  83. for (int i = 1; i < ss.Length; i++)
  84. {
  85. sb.Append(ss[i].FirstUpperCase());
  86. }
  87. return sb.ToString();
  88. }
  89. /// <summary>
  90. /// 首字母转大写
  91. /// </summary>
  92. /// <param name="str"></param>
  93. /// <returns></returns>
  94. public static string FirstUpperCase(this string str)
  95. {
  96. return string.IsNullOrEmpty(str) ? str : str[..1].ToUpper() + str[1..];
  97. }
  98. /// <summary>
  99. /// 首字母转小写
  100. /// </summary>
  101. /// <param name="str"></param>
  102. /// <returns></returns>
  103. public static string FirstLowerCase(this string str)
  104. {
  105. return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToLower() + str[1..];
  106. }
  107. /// <summary>
  108. /// 截取指定字符串中间内容
  109. /// </summary>
  110. /// <param name="sourse"></param>
  111. /// <param name="startstr"></param>
  112. /// <param name="endstr"></param>
  113. /// <returns></returns>
  114. public static string SubStringBetween(this string sourse, string startstr, string endstr)
  115. {
  116. string result = string.Empty;
  117. int startindex, endindex;
  118. try
  119. {
  120. startindex = sourse.IndexOf(startstr);
  121. if (startindex == -1)
  122. return result;
  123. string tmpstr = sourse[(startindex + startstr.Length)..];
  124. endindex = tmpstr.IndexOf(endstr);
  125. if (endindex == -1)
  126. return result;
  127. result = tmpstr.Remove(endindex);
  128. }
  129. catch (Exception ex)
  130. {
  131. Console.WriteLine("MidStrEx Err:" + ex.Message);
  132. }
  133. return result;
  134. }
  135. /// <summary>
  136. /// 转换为Pascal风格-每一个单词的首字母大写
  137. /// </summary>
  138. /// <param name="fieldName">字段名</param>
  139. /// <param name="fieldDelimiter">分隔符</param>
  140. /// <returns></returns>
  141. public static string ConvertToPascal(this string fieldName, string fieldDelimiter)
  142. {
  143. string result = string.Empty;
  144. if (fieldName.Contains(fieldDelimiter))
  145. {
  146. //全部小写
  147. string[] array = fieldName.ToLower().Split(fieldDelimiter.ToCharArray());
  148. foreach (var t in array)
  149. {
  150. //首字母大写
  151. result += t.Substring(0, 1).ToUpper() + t[1..];
  152. }
  153. }
  154. else if (string.IsNullOrWhiteSpace(fieldName))
  155. {
  156. result = fieldName;
  157. }
  158. else if (fieldName.Length == 1)
  159. {
  160. result = fieldName.ToUpper();
  161. }
  162. else if (fieldName.Length == CountUpper(fieldName))
  163. {
  164. result = fieldName[..1].ToUpper() + fieldName[1..].ToLower();
  165. }
  166. else
  167. {
  168. result = fieldName[..1].ToUpper() + fieldName[1..];
  169. }
  170. return result;
  171. }
  172. /// <summary>
  173. /// 大写字母个数
  174. /// </summary>
  175. /// <param name="str"></param>
  176. /// <returns></returns>
  177. public static int CountUpper(this string str)
  178. {
  179. int count1 = 0;
  180. char[] chars = str.ToCharArray();
  181. foreach (char num in chars)
  182. {
  183. if (num >= 'A' && num <= 'Z')
  184. {
  185. count1++;
  186. }
  187. //else if (num >= 'a' && num <= 'z')
  188. //{
  189. // count2++;
  190. //}
  191. }
  192. return count1;
  193. }
  194. /// <summary>
  195. /// 转换为Camel风格-第一个单词小写,其后每个单词首字母大写
  196. /// </summary>
  197. /// <param name="fieldName">字段名</param>
  198. /// <param name="fieldDelimiter">分隔符</param>
  199. /// <returns></returns>
  200. public static string ConvertToCamel(this string fieldName, string fieldDelimiter)
  201. {
  202. //先Pascal
  203. string result = ConvertToPascal(fieldName, fieldDelimiter);
  204. //然后首字母小写
  205. if (result.Length == 1)
  206. {
  207. result = result.ToLower();
  208. }
  209. else
  210. {
  211. result = result[..1].ToLower() + result[1..];
  212. }
  213. return result;
  214. }
  215. }
  216. }