123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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("\'", "\'\'");
- }
- /// <summary>
- /// 将字符串转换为内置字符串的样式 " -> \"
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 字符串首字母转大写
- /// </summary>
- /// <param name="val"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
|