using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Xzy.KnowledgeBase.Domain.Utils { public static class ConvertUtils { /// /// 判断是否为空,为空返回true /// /// /// public static bool IsNull(this object data) { //如果为null if (data == null) { return true; } //如果为"" if (data.GetType() == typeof(String)) { if (string.IsNullOrEmpty(data.ToString().Trim())) { return true; } } return false; } /// /// 判断是否为空,为空返回true /// /// /// public static bool IsNotNull(this object data) { //如果为null if (data == null) { return false; } //如果为"" if (data.GetType() == typeof(String)) { if (string.IsNullOrEmpty(data.ToString().Trim())) { return false; } } return true; } /// /// 判断是否为空,为空返回true /// /// /// public static bool IsNull(string data) { //如果为null if (data == null) { return true; } //如果为"" if (data.GetType() == typeof(String)) { if (string.IsNullOrEmpty(data.ToString().Trim())) { return true; } } return false; } /// /// 将obj类型转换为string /// /// /// public static string ConvertToString(this object s) { if (s == null) { return ""; } else { return Convert.ToString(s); } } /// /// object 转int32 /// /// /// public static Int32 ConvertToInt32(this object s) { int i = 0; if (s == null) { return 0; } else { int.TryParse(s.ToString(), out i); } return i; } /// /// object 转int32 /// /// /// public static Int64 ConvertToInt64(this object s) { long i = 0; if (s == null) { return 0; } else { long.TryParse(s.ToString(), out i); } return i; } /// /// 将字符串转double /// /// /// public static double ConvertToDouble(this object s) { double i = 0; if (s == null) { return 0; } else { double.TryParse(s.ToString(), out i); } return i; } /// /// 转换为datetime类型 /// /// /// public static DateTime ConvertToDateTime(this string s) { DateTime dt = new DateTime(); if (s == null || s == "") { return DateTime.Now; } DateTime.TryParse(s, out dt); return dt; } /// /// 转换为datetime类型的格式字符串 /// /// 要转换的对象 /// 格式化字符串 /// public static string ConvertToDateTime(this string s, string y) { DateTime dt = new DateTime(); DateTime.TryParse(s, out dt); return dt.ToString(y); } /// /// 将字符串转换成decimal /// /// /// public static decimal ConvertToDecimal(this object s) { decimal d = 0; if (s == null || s == "") { return 0; } Decimal.TryParse(s.ToString(), out d); return d; } /// /// decimal保留2位小数 /// public static decimal DecimalFraction(this decimal num) { return Convert.ToDecimal(num.ToString("f2")); } /// /// 替换html种的特殊字符 /// /// /// public static string ReplaceHtml(this string s) { return s.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace(""", "\""); } /// /// 流转byte /// /// /// public static byte[] StreamToByte(this Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; } public static string JsonToMarkDown(this string s) { return $"{Environment.NewLine}```json{Environment.NewLine}{s}{Environment.NewLine}```{Environment.NewLine}"; } } }