博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C#公共帮助类】 ToolsHelper帮助类
阅读量:7070 次
发布时间:2019-06-28

本文共 8205 字,大约阅读时间需要 27 分钟。

这个帮助类,目前我们只用到了两个,我就先更新这两个,后面有用到的,我会继续更新这个Helper帮助类

在Tools.cs中 有很多方法 跟Utils里是重复的,而且Utils里的方法更加新一点,大家可以删除Tools.cs里重复的,我只是删除了部分重复的。

RegexHelper

 

1 using System.Text.RegularExpressions; 2  3 namespace Common 4 { 5     ///  6     /// 操作正则表达式的公共类 7     ///      8     public class RegexHelper 9     {10         #region 验证输入字符串是否与模式字符串匹配11         /// 12         /// 验证输入字符串是否与模式字符串匹配,匹配返回true13         /// 14         /// 输入字符串15         /// 模式字符串        16         public static bool IsMatch(string input, string pattern)17         {18             return IsMatch(input, pattern, RegexOptions.IgnoreCase);19         }20 21         /// 22         /// 验证输入字符串是否与模式字符串匹配,匹配返回true23         /// 24         /// 输入的字符串25         /// 模式字符串26         /// 筛选条件27         public static bool IsMatch(string input, string pattern, RegexOptions options)28         {29             return Regex.IsMatch(input, pattern, options);30         }31         #endregion32     }33 }
View Code

 

 

Tools

 

1 using System;  2 using System.Text;  3 using System.Text.RegularExpressions;  4 using System.Collections.Generic;  5 using System.Reflection;  6 using System.Web;  7 using System.Web.Mvc;  8 using System.ComponentModel;  9  10 namespace Common 11 { 12     ///  13     /// 功能描述:共用工具类 14     ///  15     public static class Tools 16     { 17             18         #region 得到字符串长度,一个汉字长度为2 19         ///  20         /// 得到字符串长度,一个汉字长度为2 21         ///  22         /// 参数字符串 23         /// 
24 public static int StrLength(string inputString) 25 { 26 System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); 27 int tempLen = 0; 28 byte[] s = ascii.GetBytes(inputString); 29 for (int i = 0; i < s.Length; i++) 30 { 31 if ((int)s[i] == 63) 32 tempLen += 2; 33 else 34 tempLen += 1; 35 } 36 return tempLen; 37 } 38 #endregion 39 40 #region 截取指定长度字符串 41 /// 42 /// 截取指定长度字符串 43 /// 44 /// 要处理的字符串 45 /// 指定长度 46 ///
返回处理后的字符串
47 public static string ClipString(string inputString, int len) 48 { 49 bool isShowFix = false; 50 if (len % 2 == 1) 51 { 52 isShowFix = true; 53 len--; 54 } 55 System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); 56 int tempLen = 0; 57 string tempString = ""; 58 byte[] s = ascii.GetBytes(inputString); 59 for (int i = 0; i < s.Length; i++) 60 { 61 if ((int)s[i] == 63) 62 tempLen += 2; 63 else 64 tempLen += 1; 65 66 try 67 { 68 tempString += inputString.Substring(i, 1); 69 } 70 catch 71 { 72 break; 73 } 74 75 if (tempLen > len) 76 break; 77 } 78 79 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); 80 if (isShowFix && mybyte.Length > len) 81 tempString += "…"; 82 return tempString; 83 } 84 #endregion 85 86 #region 获得两个日期的间隔 87 /// 88 /// 获得两个日期的间隔 89 /// 90 /// 日期一。 91 /// 日期二。 92 ///
日期间隔TimeSpan。
93 public static TimeSpan DateDiff(DateTime DateTime1, DateTime DateTime2) 94 { 95 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); 96 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); 97 TimeSpan ts = ts1.Subtract(ts2).Duration(); 98 return ts; 99 }100 #endregion101 102 #region 格式化日期时间103 /// 104 /// 格式化日期时间105 /// 106 /// 日期时间107 /// 显示模式108 ///
0-9种模式的日期
109 public static string FormatDate(DateTime dateTime1, string dateMode)110 {111 switch (dateMode)112 {113 case "0":114 return dateTime1.ToString("yyyy-MM-dd");115 case "1":116 return dateTime1.ToString("yyyy-MM-dd HH:mm:ss");117 case "2":118 return dateTime1.ToString("yyyy/MM/dd");119 case "3":120 return dateTime1.ToString("yyyy年MM月dd日");121 case "4":122 return dateTime1.ToString("MM-dd");123 case "5":124 return dateTime1.ToString("MM/dd");125 case "6":126 return dateTime1.ToString("MM月dd日");127 case "7":128 return dateTime1.ToString("yyyy-MM");129 case "8":130 return dateTime1.ToString("yyyy/MM");131 case "9":132 return dateTime1.ToString("yyyy年MM月");133 default:134 return dateTime1.ToString();135 }136 }137 #endregion138 139 #region 得到随机日期140 /// 141 /// 得到随机日期142 /// 143 /// 起始日期144 /// 结束日期145 ///
间隔日期之间的 随机日期
146 public static DateTime GetRandomTime(DateTime time1, DateTime time2)147 {148 Random random = new Random();149 DateTime minTime = new DateTime();150 DateTime maxTime = new DateTime();151 152 System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);153 154 // 获取两个时间相隔的秒数155 double dTotalSecontds = ts.TotalSeconds;156 int iTotalSecontds = 0;157 158 if (dTotalSecontds > System.Int32.MaxValue)159 {160 iTotalSecontds = System.Int32.MaxValue;161 }162 else if (dTotalSecontds < System.Int32.MinValue)163 {164 iTotalSecontds = System.Int32.MinValue;165 }166 else167 {168 iTotalSecontds = (int)dTotalSecontds;169 }170 171 172 if (iTotalSecontds > 0)173 {174 minTime = time2;175 maxTime = time1;176 }177 else if (iTotalSecontds < 0)178 {179 minTime = time1;180 maxTime = time2;181 }182 else183 {184 return time1;185 }186 187 int maxValue = iTotalSecontds;188 189 if (iTotalSecontds <= System.Int32.MinValue)190 maxValue = System.Int32.MinValue + 1;191 192 int i = random.Next(System.Math.Abs(maxValue));193 194 return minTime.AddSeconds(i);195 }196 /// 197 /// 获取时间戳198 /// 199 public static string GetRandomTimeSpan()200 {201 TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);202 return Convert.ToInt64(ts.TotalSeconds).ToString();203 }204 #endregion205 206 #region HTML转行成TEXT207 /// 208 /// HTML转行成TEXT209 /// 210 /// 211 ///
212 public static string HtmlToTxt(string strHtml)213 {214 string[] aryReg ={215 @"
]*?>.*?",216 @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",217 @"([\r\n])[\s]+",218 @"&(quot|#34);",219 @"&(amp|#38);",220 @"&(lt|#60);",221 @"&(gt|#62);", 222 @"&(nbsp|#160);", 223 @"&(iexcl|#161);",224 @"&(cent|#162);",225 @"&(pound|#163);",226 @"&(copy|#169);",227 @"&#(\d+);",228 @"-->",229 @"
View Code

 

 

原创文章 转载请尊重劳动成果 

 

转载于:https://www.cnblogs.com/yuangang/p/5484304.html

你可能感兴趣的文章
Java基础加密之MD5加密算法
查看>>
盛夏光年
查看>>
Android 沉浸式状态栏(像IOS那样的状态栏与应用统一颜色样式)
查看>>
RHCS集群服务 7.10
查看>>
windows 使用vnc图形化界面远程连接阿里云ubuntu 16.04云服务器
查看>>
linux和CentOS是什么关系;CentOS和RHEL是什么关系
查看>>
samba
查看>>
利用Python网络爬虫抓取微信好友的签名及其可视化展示
查看>>
Linux-Nginx代理
查看>>
计算机的系统组成简介---运维笔记
查看>>
Liunx nginx 的使用方法及模块
查看>>
alter system switch logfile与alter system archive log current
查看>>
H3C MSR3020路由NQA实例配置
查看>>
数据类型转换的常见错误
查看>>
DBA——表级数据恢复之路(一) 请下载附件查看
查看>>
我的友情链接
查看>>
hello,blog!
查看>>
搭建最简单ceph环境(入门)
查看>>
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers
查看>>
nagios常见的故障说明
查看>>