123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Web;
- namespace ETD.Data
- {
- public class Text
- {
- #region 清除给定字符串中的回车及换行符
- /// <summary>
- /// 清除给定字符串中的回车及换行符
- /// </summary>
- /// <param name="str">要清除的字符串</param>
- /// <returns>清除后返回的字符串</returns>
- public static string ClearBR(string str)
- {
- Regex r = null;
- Match m = null;
- r = new Regex(@"(\r\n)", RegexOptions.IgnoreCase);
- for (m = r.Match(str); m.Success; m = m.NextMatch())
- {
- str = str.Replace(m.Groups[0].ToString(), "");
- }
- return str;
- }
- #endregion
- #region 清除html字符
- public static string ClearHtml(string strHtml)
- {
- if (strHtml != "")
- {
- Regex r = null;
- Match m = null;
- r = new Regex(@"<\/?[^>]*>", RegexOptions.IgnoreCase);
- for (m = r.Match(strHtml); m.Success; m = m.NextMatch())
- {
- strHtml = strHtml.Replace(m.Groups[0].ToString(), "");
- }
- }
- return strHtml;
- }
- #endregion
- #region 生成内码
- /// <summary>
- /// 生成系统需要的内码
- /// </summary>
- /// <returns>返回一个22位的内码</returns>
- public static string CodeBuild()
- {
- try
- {
- System.Threading.Thread.Sleep(15);
- string code = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
- if (code.Length < 17)
- {
- for (int i = 0; i < 17 - code.Length; i++)
- {
- code = code + "0";
- }
- }
- Random Rnd = new Random();
- string ran = Rnd.Next(10000000, 99999999).ToString();
- code = code + ran;
- if (code.Length < 25)
- {
- for (int i = 0; i < 25 - code.Length; i++)
- {
- code = code + "8";
- }
- }
- return code;
- }
- catch (Exception e)
- {
- throw (e);
- }
- }
- #endregion
- #region 字符串如果操过指定长度则将超出的部分用指定字符串代替
- /// <summary>
- /// 字符串如果操过指定长度则将超出的部分用指定字符串代替
- /// </summary>
- /// <param name="strStr">要检查的字符串</param>
- /// <param name="strLength">指定长度</param>
- /// <param name="strReplace">用于替换的字符串</param>
- /// <returns>截取后的字符串</returns>
- public static string CutString(string strStr, int strLength, string strReplace)
- {
- string strMyStr = strStr;
- if (strLength < 0)
- {
- return strMyStr;
- }
- byte[] bsSrcString = Encoding.Default.GetBytes(strStr);
- if (bsSrcString.Length <= strLength)
- {
- return strMyStr;
- }
- int nRealLength = strLength;
- int[] anResultFlag = new int[strLength];
- byte[] bsResult = null;
- int nFlag = 0;
- for (int i = 0; i < strLength; i++)
- {
- if (bsSrcString[i] > 0x7f)
- {
- nFlag++;
- if (nFlag == 3)
- {
- nFlag = 1;
- }
- }
- else
- {
- nFlag = 0;
- }
- anResultFlag[i] = nFlag;
- }
- if ((bsSrcString[strLength - 1] > 0x7f) && (anResultFlag[strLength - 1] == 1))
- {
- nRealLength = strLength + 1;
- }
- bsResult = new byte[nRealLength];
- Array.Copy(bsSrcString, bsResult, nRealLength);
- return (Encoding.Default.GetString(bsResult) + strReplace);
- }
- #endregion
- #region 替换html字符
- /// <summary>
- /// 替换html字符
- /// </summary>
- public static string EncodeHtml(string strHtml)
- {
- if (strHtml != "")
- {
- strHtml = strHtml.Replace(",", "&def");
- strHtml = strHtml.Replace("'", "&dot");
- strHtml = strHtml.Replace(";", "&dec");
- return strHtml;
- }
- return "";
- }
- #endregion
- #region 生成指定位数的随机数
- public static string GenerateRandNum(int Length, RandEnum randtype)
- {
- string code = "";
- Random Rnd = new Random();
- if (code.Length < Length)
- {
- switch (randtype)
- {
- case RandEnum.Int:
- for (int i = 0; i < Length - code.Length; i++)
- {
- code = code + Rnd.Next(0, 9).ToString();
- }
- break;
- case RandEnum.DateTime:
- code = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
- for (int i = 0; i < Length - code.Length; i++)
- {
- code = code + Rnd.Next(0, 9).ToString();
- }
- break;
- default:
- break;
- }
- }
- return code;
- }
- public enum RandEnum
- {
- Int,
- DateTime
- }
- #endregion
- #region 取页面Get传递的参数
- /// <summary>
- /// 取URL传递的参数
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static string Get(string name)
- {
- string text1 = HttpContext.Current.Request.QueryString[name];
- return ((text1 == null) ? string.Empty : text1.Trim());
- }
- /// <summary>
- /// 取URL传递的参数
- /// </summary>
- /// <param name="name">指定参数名</param>
- /// <param name="chkType">类型</param>
- /// <returns></returns>
- public static string Get(string name, CheckGetEnum chkType)
- {
- string text1 = Get(name);
- bool flag1 = false;
- switch (chkType)
- {
- case CheckGetEnum.Int:
- flag1 = RegExp.IsNumber(text1);
- break;
- case CheckGetEnum.Safety:
- flag1 = RegExp.IsSafety(text1);
- break;
- case CheckGetEnum.Json:
- flag1 = RegExp.IsJsonSafety(text1);
- break;
- default:
- flag1 = true;
- break;
- }
- if (!flag1)
- {
- throw new Exception("hc1305");
- }
- return text1;
- }
- #endregion
- #region 取提交Post页面指定值
- /// <summary>
- /// 取提交页面指定值
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static string Post(string name)
- {
- string text1 = HttpContext.Current.Request.Form[name];
- return ((text1 == null) ? string.Empty : text1.Trim());
- }
- /// <summary>
- /// 取提交页面指定值
- /// </summary>
- /// <param name="name"></param>
- /// <param name="chkType">类型</param>
- /// <returns></returns>
- public static string Post(string name, CheckGetEnum chkType)
- {
- string text1 = HttpContext.Current.Request.Form[name];
- if (text1 == null)
- {
- return string.Empty;
- }
- bool flag1 = false;
- switch (chkType)
- {
- case CheckGetEnum.Int:
- flag1 = RegExp.IsNumber(text1);
- break;
- case CheckGetEnum.Safety:
- flag1 = RegExp.IsSafety(text1);
- break;
- case CheckGetEnum.Json:
- flag1 = RegExp.IsJsonSafety(text1);
- break;
- default:
- flag1 = true;
- break;
- }
- if (!flag1)
- {
- throw new Exception("hc1305");
- }
- return text1.Trim();
- }
- /// <summary>
- /// 枚举
- /// </summary>
- public enum CheckGetEnum
- {
- Int,
- Safety,
- Json
- }
- #endregion
- #region 返回标准日期格式string
- /// <summary>
- /// 返回标准日期格式string
- /// </summary>
- public static string GetDate()
- {
- return DateTime.Now.ToString("yyyy-MM-dd");
- }
- #endregion
- #region 返回指定日期格式
- /// <summary>
- /// 返回指定日期格式
- /// </summary>
- public static string GetDate(string datetimestr, string replacestr)
- {
- if (datetimestr == null)
- {
- return replacestr;
- }
- if (datetimestr.Equals(""))
- {
- return replacestr;
- }
- try
- {
- datetimestr = Convert.ToDateTime(datetimestr).ToString("yyyy-MM-dd").Replace("1900-01-01", replacestr);
- }
- catch
- {
- return replacestr;
- }
- return datetimestr;
- }
- #endregion
- #region 返回标准时间格式string
- /// <summary>
- /// 返回标准时间格式string:yyyy-MM-dd HH:mm:ss
- /// </summary>
- public static string GetDateTime()
- {
- return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- }
- #endregion
- #region 获取文件内容
- /// <summary>
- /// 获取文件内容
- /// </summary>
- /// <param name="strPath">文件路径</param>
- /// <returns>文件内容</returns>
- public static string GetFileContent(string strPath)
- {
- StreamReader fileStream = new StreamReader(strPath, Encoding.Default);
- string ct = fileStream.ReadToEnd();
- fileStream.Close();
- return ct;
- }
- #endregion
- #region 获得当前绝对路径
- /// <summary>
- /// 获得当前绝对路径
- /// </summary>
- /// <param name="strPath">指定的路径</param>
- /// <returns>绝对路径</returns>
- public static string GetSitePath(string strPath)
- {
- if (HttpContext.Current != null)
- {
- return HttpContext.Current.Server.MapPath(strPath);
- }
- return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
- }
- #endregion
- #region 获取字符串长度,注意,1个汉字长度为2
- /// <summary>
- /// 获取字符串长度,注意,1个汉字长度为2
- /// </summary>
- /// <param name="str">要检查字符串</param>
- /// <returns>字符串长度</returns>
- public static int GetStringLength(string str)
- {
- return Encoding.Default.GetBytes(str).Length;
- }
- #endregion
- #region MD5函数
- /// <summary>
- /// MD5函数
- /// </summary>
- /// <param name="str">原始字符串</param>
- /// <returns>MD5结果</returns>
- public static string MD5(string str)
- {
- byte[] b = Encoding.Default.GetBytes(str);
- b = new MD5CryptoServiceProvider().ComputeHash(b);
- string ret = "";
- for (int i = 0; i < b.Length; i++)
- {
- ret = ret + b[i].ToString("x").PadLeft(2, '0');
- }
- return ret;
- }
- #endregion
- #region 移除Html标记
- /// <summary>
- /// 移除Html标记
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- public static string RemoveHtml(string content)
- {
- string regexstr = "<[^>]*>";
- return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
- }
- #endregion
- #region 删除字符串尾部的回车/换行/空格
- /// <summary>
- /// 删除字符串尾部的回车/换行/空格
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string RTrim(string str)
- {
- for (int i = str.Length; i >= 0; i--)
- {
- char ts = str[i];
- if (!ts.Equals(" "))
- {
- ts = str[i];
- }
- if (ts.Equals("\r") || (ts = str[i]).Equals("\n"))
- {
- str.Remove(i, 1);
- }
- }
- return str;
- }
- #endregion
- #region SHA256函数
- /// <summary>
- /// SHA256函数
- /// </summary>
- /// /// <param name="str">原始字符串</param>
- /// <returns>SHA256结果</returns>
- public static string SHA256(string str)
- {
- byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
- SHA256Managed Sha256 = new SHA256Managed();
- byte[] Result = Sha256.ComputeHash(SHA256Data);
- return Convert.ToBase64String(Result); //返回长度为44字节的字符串
- }
- #endregion
- #region 生成指定数量的html空格符号
- /// <summary>
- /// 生成指定数量的html空格符号
- /// </summary>
- public static string Spaces(int nSpaces)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < nSpaces; i++)
- {
- sb.Append(" ");
- }
- return sb.ToString();
- }
- #endregion
- #region 生成指定数量的html符号
- /// <summary>
- /// 生成指定数量的html符号
- /// </summary>
- public static string Spaces(int nSpaces, string strHtml)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < nSpaces; i++)
- {
- sb.Append(strHtml);
- }
- return sb.ToString();
- }
- #endregion
- #region 替换回车换行符为html换行符
- /// <summary>
- /// 替换回车换行符为html换行符
- /// </summary>
- public static string StrFormat(string str)
- {
- string str2;
- if (str == null)
- {
- str2 = "";
- }
- else
- {
- str = str.Replace("\r\n", "<br />");
- str = str.Replace("\n", "<br />");
- str2 = str;
- }
- return str2;
- }
- #endregion
- #region string型转换为int型
- /// <summary>
- /// string型转换为int型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int StrToInt(object strValue, int defValue)
- {
- if ((strValue == null) || (strValue.ToString() == string.Empty) || (strValue.ToString().Length > 10))
- {
- return defValue;
- }
- string val = strValue.ToString();
- string firstletter = val[0].ToString();
- if (val.Length == 10 && RegExp.IsNumber(firstletter) && int.Parse(firstletter) > 1)
- {
- return defValue;
- }
- else if (val.Length == 10 && !RegExp.IsNumber(firstletter))
- {
- return defValue;
- }
- int intValue = defValue;
- if (strValue != null)
- {
- bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString());
- if (IsInt)
- {
- intValue = Convert.ToInt32(strValue);
- }
- }
- return intValue;
- }
- #endregion
- #region 返回 URL 字符串的编码结果
- /// <summary>
- /// 返回 URL 字符串的编码结果
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>解码结果</returns>
- public static string UrlDecode(string str)
- {
- return HttpUtility.UrlDecode(str);
- }
- #endregion
- #region 返回 HTML 字符串的编码结果
- /// <summary>
- /// 返回 HTML 字符串的编码结果
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>编码结果</returns>
- public static string HtmlEncode(string str)
- {
- return HttpUtility.HtmlEncode(str);
- }
- #endregion
- #region 返回 HTML 字符串的解码结果
- /// <summary>
- /// 返回 HTML 字符串的解码结果
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>解码结果</returns>
- public static string HtmlDecode(string str)
- {
- return HttpUtility.HtmlDecode(str);
- }
- #endregion
-
-
- }
- }
|