StringExtensions.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Newtonsoft.Json;
  2. namespace etoy
  3. {
  4. static class StringExtensions
  5. {
  6. public static string ToTableName(this string str)
  7. {
  8. if (string.IsNullOrEmpty(str))
  9. return str;
  10. return str.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim();
  11. }
  12. public static string ToFieldType(this string str)
  13. {
  14. if (string.IsNullOrEmpty(str))
  15. return str;
  16. return str.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim().ToLower();
  17. }
  18. public static string ToFieldName(this string str)
  19. {
  20. if (string.IsNullOrEmpty(str))
  21. return str;
  22. return str.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim();
  23. }
  24. public static string ToTags(this string str)
  25. {
  26. if (string.IsNullOrEmpty(str))
  27. return str;
  28. return str.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim().ToLower();
  29. }
  30. public static string ToCellValue(this string str)
  31. {
  32. if (string.IsNullOrEmpty(str))
  33. return str;
  34. return str.ToString();
  35. }
  36. public static string ToDescription(this string str)
  37. {
  38. if (string.IsNullOrEmpty(str))
  39. return str;
  40. return str.ToString();
  41. }
  42. public static string ToPath(this string str)
  43. {
  44. if (string.IsNullOrEmpty(str))
  45. return str;
  46. return str.Replace("\\", "/");
  47. }
  48. public static string ToSqlString(this string str)
  49. {
  50. if (string.IsNullOrEmpty(str))
  51. return str;
  52. return str.Replace("\'", "\'\'");
  53. }
  54. /// <summary>
  55. /// 将字符串转换为内置字符串的样式 " -> \"
  56. /// </summary>
  57. /// <param name="str"></param>
  58. /// <returns></returns>
  59. public static string ToInlayString(this string str)
  60. {
  61. if (string.IsNullOrEmpty(str))
  62. return str;
  63. return str.Replace("\"", "\\\"");
  64. }
  65. public static bool IsJsonString(this string str)
  66. {
  67. bool bRet = true;
  68. try
  69. {
  70. _ = JsonConvert.DeserializeObject(str);
  71. }
  72. catch (Exception)
  73. {
  74. bRet = false;
  75. }
  76. return bRet;
  77. }
  78. public static bool JsonToObject(this string str, Type type, out object? result)
  79. {
  80. var bRet = true;
  81. try
  82. {
  83. result = JsonConvert.DeserializeObject(str, type);
  84. }
  85. catch (Exception)
  86. {
  87. bRet = false;
  88. result = null;
  89. }
  90. return bRet;
  91. }
  92. public static bool IsKsonString(this string str)
  93. {
  94. if (string.IsNullOrEmpty(str))
  95. return false;
  96. int indent = 0;
  97. bool start = false, dot = false;
  98. bool end, invalid;
  99. for (int i = 0, length = str.Length; i < length; i++)
  100. {
  101. char c = str[i];
  102. // check end first
  103. end = c == '}' || c == ']';
  104. invalid = dot && end;
  105. if (invalid) return false;
  106. // set dot second
  107. var d = c == ',';
  108. if (dot && d) return false;
  109. dot = d;
  110. // check start last
  111. invalid = dot && start;
  112. if (invalid) return false;
  113. start = c == '[' || c == '{';
  114. if (start) indent++;
  115. if (end) indent--;
  116. // check all chars
  117. //invalid = !(c >= '0' && c <= '9'
  118. // || c >= 'A' && c <= 'Z'
  119. // || c >= 'a' && c <= 'z'
  120. // || c == ' '
  121. // || c == '_'
  122. // || start
  123. // || end
  124. // || dot);
  125. if (invalid) return false;
  126. }
  127. return indent == 0;
  128. }
  129. /// <summary>
  130. /// 字符串首字母转大写
  131. /// </summary>
  132. /// <param name="val"></param>
  133. /// <returns></returns>
  134. public static string ToTitleCase(this string val)
  135. {
  136. //return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(val);
  137. if (string.IsNullOrEmpty(val))
  138. {
  139. return val;
  140. }
  141. return char.ToUpper(val[0]) + val.Substring(1);
  142. }
  143. }
  144. }