CommUtils.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Reflection;
  7. namespace CSharpUtil
  8. {
  9. static class CommUtils
  10. {
  11. public static string var_dump(this object info)
  12. {
  13. StringBuilder sb = new StringBuilder();
  14. Type t = info.GetType();
  15. PropertyInfo[] props = t.GetProperties();
  16. sb.AppendFormat("{0,-18} {1}", "Name", "Value");
  17. sb.AppendLine();
  18. foreach (PropertyInfo prop in props)
  19. {
  20. sb.AppendFormat("{0,-18} {1}", prop.Name, prop.GetValue(info, null).ToString());
  21. sb.AppendLine();
  22. }
  23. return sb.ToString();
  24. }
  25. }
  26. #region ` 字符串扩展 `
  27. public static class StringExtensionUtils
  28. {
  29. /// <summary>
  30. /// 字符串扩展:正则替换方法
  31. /// </summary>
  32. /// <param name="str"></param>
  33. /// <param name="pattern"></param>
  34. /// <param name="replacement"></param>
  35. /// <returns></returns>
  36. public static string RegReplace(this string str, string pattern, string replacement)
  37. {
  38. return Regex.Replace(str, pattern, replacement);
  39. }
  40. readonly static Regex reg = new Regex(@"(?<=""|:|,|\{|\}|\[|\])\s+(?=""|:|,|\{|\}|\[|\]|null|\d)");
  41. /// <summary>
  42. /// 字符串扩展:压缩json字符串的功能(去除多余的空白字符)
  43. /// </summary>
  44. /// <param name="str"></param>
  45. /// <returns></returns>
  46. [Obsolete("鉴于次函数的性能问题,不建议继续使用.--gwang 2022.3.4")]
  47. public static string JsMin(this string str)
  48. {
  49. return reg.Replace(str, "");
  50. }
  51. }
  52. #endregion
  53. }