using Newtonsoft.Json; namespace etoy { static class StringExtensions { public static string ToTableName(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim(); } public static string ToFieldType(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim().ToLower(); } public static string ToFieldName(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim(); } public static string ToTags(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim().ToLower(); } public static string ToCellValue(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.ToString(); } public static string ToDescription(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.ToString(); } public static string ToPath(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.Replace("\\", "/"); } public static string ToSqlString(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.Replace("\'", "\'\'"); } /// /// 将字符串转换为内置字符串的样式 " -> \" /// /// /// public static string ToInlayString(this string str) { if (string.IsNullOrEmpty(str)) return str; return str.Replace("\"", "\\\""); } public static bool IsJsonString(this string str) { bool bRet = true; try { _ = JsonConvert.DeserializeObject(str); } catch (Exception) { bRet = false; } return bRet; } public static bool JsonToObject(this string str, Type type, out object? result) { var bRet = true; try { result = JsonConvert.DeserializeObject(str, type); } catch (Exception) { bRet = false; result = null; } return bRet; } public static bool IsKsonString(this string str) { if (string.IsNullOrEmpty(str)) return false; int indent = 0; bool start = false, dot = false; bool end, invalid; for (int i = 0, length = str.Length; i < length; i++) { char c = str[i]; // check end first end = c == '}' || c == ']'; invalid = dot && end; if (invalid) return false; // set dot second var d = c == ','; if (dot && d) return false; dot = d; // check start last invalid = dot && start; if (invalid) return false; start = c == '[' || c == '{'; if (start) indent++; if (end) indent--; // check all chars //invalid = !(c >= '0' && c <= '9' // || c >= 'A' && c <= 'Z' // || c >= 'a' && c <= 'z' // || c == ' ' // || c == '_' // || start // || end // || dot); if (invalid) return false; } return indent == 0; } /// /// 字符串首字母转大写 /// /// /// public static string ToTitleCase(this string val) { //return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(val); if (string.IsNullOrEmpty(val)) { return val; } return char.ToUpper(val[0]) + val.Substring(1); } } }