using System.Globalization; using System.Text; using System.Text.RegularExpressions; namespace DS.Module.Core.Extensions; public static class StringExtensions { /// /// sql /// /// /// public static bool IsSqlInjection(string input) { string[] sqlCheckList = { "TRUNCATE", "INSERT", "UPDATE", "DELETE", "DROP", "--", ";", "'" }; foreach (string item in sqlCheckList) { if (input.IndexOf(item, StringComparison.OrdinalIgnoreCase) >= 0) { return true; } } return false; } /// /// 以指定字符串作为分隔符将指定字符串分隔成数组 /// /// 要分割的字符串 /// 字符串类型的分隔符 /// 是否移除数据中元素为空字符串的项 /// 分割后的数据 public static string[] Split(this string value, string strSplit, bool removeEmptyEntries = false) { value.NotNullOrEmpty(nameof(value)); strSplit.NotNullOrEmpty(nameof(strSplit)); return value.Split(new[] { strSplit }, removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None); } /// /// 将字符中拼接 /// /// /// /// /// /// public static string StrToJoin(this string value, string left, string right, string separator = ",") { value.NotNullOrEmpty(nameof(value)); left.NotNullOrEmpty(nameof(left)); right.NotNullOrEmpty(nameof(right)); StringBuilder sb = new StringBuilder(); if (!value.IsNullOrEmpty()) { foreach (var item in value.Split(separator)) { sb.AppendFormat("{0}{1}{2}{3}", left, item, right, separator); } } return sb.ToString().TrimEnd(separator.ToCharArray()); } /// ///把字符串转成SQL中IN /// /// 要转换的值 /// 左边符 /// 右边符 /// in里面中间分割符:"'a','b'" /// 值分割符如:"a,b,c,d" /// 返回组装好的值,例如"'a','b'" public static string ToSqlIn(this string value, string left = "'", string right = "'", string inMiddleSeparator = ",", string valueSeparator = ",") { value.NotNullOrEmpty(nameof(value)); StringBuilder sb = new StringBuilder(); if (!value.IsNullOrEmpty()) { foreach (var item in value.Split(valueSeparator)) { sb.AppendFormat("{0}{1}{2}{3}", left, item, right, inMiddleSeparator); } } else { return value; } return sb.ToString().TrimEnd(inMiddleSeparator.ToCharArray()); } /// /// 是否为空或者为null或者空格 /// /// 要判断的值 /// 返回true/false public static bool IsNullOrWhiteSpace(this string value) { return string.IsNullOrWhiteSpace(value); } /// /// 是否为空或者为null /// /// 要判断的值 /// 返回true/false public static bool IsNullOrEmpty(this string value) { return string.IsNullOrEmpty(value); } /// /// 判断是否数字 /// /// /// public static bool IsInt(this string value) { return Regex.IsMatch(value, @"^[0-9]*$"); } /// /// 在指定的输入字符串中搜索指定的正则表达式的第一个匹配项 /// /// 要搜索匹配项的字符串 /// 要匹配的正则表达式模式 /// 一个对象,包含有关匹配项的信息 public static string Match(this string value, string pattern) { if (value == null) { return null; } return Regex.Match(value, pattern).Value; } public static string FormatWith(this string format, params object[] args) { format.NotNull("format"); return string.Format(CultureInfo.CurrentCulture, format, args); } /// /// 将驼峰字符串的第一个字符小写 /// public static string LowerFirstChar(this string str) { if (string.IsNullOrEmpty(str) || !char.IsUpper(str[0])) { return str; } if (str.Length == 1) { return char.ToLower(str[0]).ToString(); } return char.ToLower(str[0]) + str.Substring(1, str.Length - 1); } }