字符串工具类
1 /// <summary> 2 /// SQL条件拼接 3 /// </summary> 4 /// <param name="str"></param> 5 /// <param name="condition"></param> 6 /// <returns></returns> 7 public static string If(this string str, bool condition) 8 { 9 return condition ? str : string.Empty; 10 } 11 /// <summary> 12 /// 判断是否为空 13 /// </summary> 14 /// <param name="str"></param> 15 /// <returns></returns> 16 public static bool IfNotEmpty(this string str) 17 { 18 return !string.IsNullOrEmpty(str); 19 } 20 21 /// <summary> 22 /// 注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//("); 23 /// </summary> 24 /// <param name="input"></param> 25 /// <param name="oldValue"></param> 26 /// <param name="newValue"></param> 27 /// <returns></returns> 28 public static string ReplaceFirst(this string input, string oldValue, string newValue) 29 { 30 Regex regEx = new Regex(oldValue, RegexOptions.Multiline); 31 return regEx.Replace(input, newValue == null ? "" : newValue, 1); 32 } 33 34 /// <summary> 35 /// 骆驼峰转下划线 36 /// </summary> 37 /// <param name="name"></param> 38 /// <returns></returns> 39 public static string ToSmallCamelCase(string name) 40 { 41 var stringBuilder = new StringBuilder(); 42 stringBuilder.Append(name.Substring(0, 1).ToLower()); 43 44 for (var i = 0; i < name.Length; i++) 45 { 46 if (i == 0) 47 { 48 stringBuilder.Append(name.Substring(0, 1).ToLower()); 49 } 50 else 51 { 52 if (name[i] >= 'A' && name[i] <= 'Z') 53 { 54 stringBuilder.Append($"_{name.Substring(i, 1).ToLower()}"); 55 } 56 else 57 { 58 stringBuilder.Append(name[i]); 59 } 60 } 61 } 62 63 return stringBuilder.ToString(); 64 } 65 66 /// <summary> 67 /// 下划线命名转驼峰命名 68 /// </summary> 69 /// <param name="underscore"></param> 70 /// <returns></returns> 71 public static string UnderScoreToCamelCase(this string underscore) 72 { 73 string[] ss = underscore.Split("_"); 74 if (ss.Length == 1) 75 { 76 return underscore; 77 } 78 79 StringBuilder sb = new(); 80 sb.Append(ss[0]); 81 for (int i = 1; i < ss.Length; i++) 82 { 83 sb.Append(ss[i].FirstUpperCase()); 84 } 85 86 return sb.ToString(); 87 } 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 99 /// <summary> 100 /// 首字母转小写 101 /// </summary> 102 /// <param name="str"></param> 103 /// <returns></returns> 104 public static string FirstLowerCase(this string str) 105 { 106 return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToLower() + str[1..]; 107 } 108 109 /// <summary> 110 /// 截取指定字符串中间内容 111 /// </summary> 112 /// <param name="sourse"></param> 113 /// <param name="startstr"></param> 114 /// <param name="endstr"></param> 115 /// <returns></returns> 116 public static string SubStringBetween(this string sourse, string startstr, string endstr) 117 { 118 string result = string.Empty; 119 int startindex, endindex; 120 try 121 { 122 startindex = sourse.IndexOf(startstr); 123 if (startindex == -1) 124 return result; 125 string tmpstr = sourse[(startindex + startstr.Length)..]; 126 endindex = tmpstr.IndexOf(endstr); 127 if (endindex == -1) 128 return result; 129 result = tmpstr.Remove(endindex); 130 } 131 catch (Exception ex) 132 { 133 Console.WriteLine("MidStrEx Err:" + ex.Message); 134 } 135 return result; 136 } 137 138 /// <summary> 139 /// 转换为Pascal风格-每一个单词的首字母大写 140 /// </summary> 141 /// <param name="fieldName">字段名</param> 142 /// <param name="fieldDelimiter">分隔符</param> 143 /// <returns></returns> 144 public static string ConvertToPascal(this string fieldName, string fieldDelimiter) 145 { 146 string result = string.Empty; 147 if (fieldName.Contains(fieldDelimiter)) 148 { 149 //全部小写 150 string[] array = fieldName.ToLower().Split(fieldDelimiter.ToCharArray()); 151 foreach (var t in array) 152 { 153 //首字母大写 154 result += t.Substring(0, 1).ToUpper() + t[1..]; 155 } 156 } 157 else if (string.IsNullOrWhiteSpace(fieldName)) 158 { 159 result = fieldName; 160 } 161 else if (fieldName.Length == 1) 162 { 163 result = fieldName.ToUpper(); 164 } 165 else if (fieldName.Length == fieldName.CountUpper()) 166 { 167 result = fieldName[..1].ToUpper() + fieldName[1..].ToLower(); 168 } 169 else 170 { 171 result = fieldName[..1].ToUpper() + fieldName[1..]; 172 } 173 return result; 174 } 175 176 /// <summary> 177 /// 大写字母个数 178 /// </summary> 179 /// <param name="str"></param> 180 /// <returns></returns> 181 public static int CountUpper(this string str) 182 { 183 int count1 = 0; 184 char[] chars = str.ToCharArray(); 185 foreach (char num in chars) 186 { 187 if (num >= 'A' && num <= 'Z') 188 { 189 count1++; 190 } 191 //else if (num >= 'a' && num <= 'z') 192 //{ 193 // count2++; 194 //} 195 } 196 return count1; 197 } 198 199 /// <summary> 200 /// 转换为Camel风格-第一个单词小写,其后每个单词首字母大写 201 /// </summary> 202 /// <param name="fieldName">字段名</param> 203 /// <param name="fieldDelimiter">分隔符</param> 204 /// <returns></returns> 205 public static string ConvertToCamel(this string fieldName, string fieldDelimiter) 206 { 207 //先Pascal 208 string result = fieldName.ConvertToPascal(fieldDelimiter); 209 //然后首字母小写 210 if (result.Length == 1) 211 { 212 result = result.ToLower(); 213 } 214 else 215 { 216 result = result[..1].ToLower() + result[1..]; 217 } 218 219 return result; 220 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构