字符串扩展方法
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 using System.Text; 7 8 public static class StringExtend 9 { 10 public static DateTime ToDatetime(this string str) 11 { 12 if (string.IsNullOrEmpty(str)) 13 return DateTime.MinValue; 14 else 15 return Convert.ToDateTime(str); 16 } 17 18 public static DateTime? ToNullableDatetime(this string str) 19 { 20 if (string.IsNullOrEmpty(str)) 21 return null; 22 else 23 return Convert.ToDateTime(str); 24 } 25 26 public static int ToInt32(this string str) 27 { 28 if (string.IsNullOrEmpty(str)) 29 return int.MinValue; 30 else 31 return Convert.ToInt16(str); 32 } 33 34 public static int? ToInt32(this string str) 35 { 36 if (string.IsNullOrEmpty(str)) 37 return null; 38 else 39 return Convert.ToInt16(str); 40 } 41 public static string HtmlDecode(this string str) 42 { 43 StringBuilder sb = new StringBuilder(str); 44 sb.Replace("<br />", "\n"); 45 sb.Replace("\r", ""); 46 sb.Replace(" ", "\t"); 47 sb.Replace(" ", " "); 48 sb.Replace("'", "\'"); 49 sb.Replace(""", "\""); 50 sb.Replace(">", ">"); 51 sb.Replace("<", "<"); 52 sb.Replace("&", "&"); 53 return sb.ToString(); 54 } 55 56 public static string HtmlEncode(this string str) 57 { 58 StringBuilder sb = new StringBuilder(str); 59 sb.Replace("&", "&"); 60 sb.Replace("<", "<"); 61 sb.Replace(">", ">"); 62 sb.Replace("\"", """); 63 sb.Replace("\'", "'"); 64 sb.Replace(" ", " "); 65 sb.Replace("\t", " "); 66 sb.Replace("\r", ""); 67 sb.Replace("\n", "<br />"); 68 return sb.ToString(); 69 } 70 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现