Text.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. using System.Web;
  8. namespace ETD.Data
  9. {
  10. public class Text
  11. {
  12. #region 清除给定字符串中的回车及换行符
  13. /// <summary>
  14. /// 清除给定字符串中的回车及换行符
  15. /// </summary>
  16. /// <param name="str">要清除的字符串</param>
  17. /// <returns>清除后返回的字符串</returns>
  18. public static string ClearBR(string str)
  19. {
  20. Regex r = null;
  21. Match m = null;
  22. r = new Regex(@"(\r\n)", RegexOptions.IgnoreCase);
  23. for (m = r.Match(str); m.Success; m = m.NextMatch())
  24. {
  25. str = str.Replace(m.Groups[0].ToString(), "");
  26. }
  27. return str;
  28. }
  29. #endregion
  30. #region 清除html字符
  31. public static string ClearHtml(string strHtml)
  32. {
  33. if (strHtml != "")
  34. {
  35. Regex r = null;
  36. Match m = null;
  37. r = new Regex(@"<\/?[^>]*>", RegexOptions.IgnoreCase);
  38. for (m = r.Match(strHtml); m.Success; m = m.NextMatch())
  39. {
  40. strHtml = strHtml.Replace(m.Groups[0].ToString(), "");
  41. }
  42. }
  43. return strHtml;
  44. }
  45. #endregion
  46. #region 生成内码
  47. /// <summary>
  48. /// 生成系统需要的内码
  49. /// </summary>
  50. /// <returns>返回一个22位的内码</returns>
  51. public static string CodeBuild()
  52. {
  53. try
  54. {
  55. System.Threading.Thread.Sleep(15);
  56. string code = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
  57. if (code.Length < 17)
  58. {
  59. for (int i = 0; i < 17 - code.Length; i++)
  60. {
  61. code = code + "0";
  62. }
  63. }
  64. Random Rnd = new Random();
  65. string ran = Rnd.Next(10000000, 99999999).ToString();
  66. code = code + ran;
  67. if (code.Length < 25)
  68. {
  69. for (int i = 0; i < 25 - code.Length; i++)
  70. {
  71. code = code + "8";
  72. }
  73. }
  74. return code;
  75. }
  76. catch (Exception e)
  77. {
  78. throw (e);
  79. }
  80. }
  81. #endregion
  82. #region 字符串如果操过指定长度则将超出的部分用指定字符串代替
  83. /// <summary>
  84. /// 字符串如果操过指定长度则将超出的部分用指定字符串代替
  85. /// </summary>
  86. /// <param name="strStr">要检查的字符串</param>
  87. /// <param name="strLength">指定长度</param>
  88. /// <param name="strReplace">用于替换的字符串</param>
  89. /// <returns>截取后的字符串</returns>
  90. public static string CutString(string strStr, int strLength, string strReplace)
  91. {
  92. string strMyStr = strStr;
  93. if (strLength < 0)
  94. {
  95. return strMyStr;
  96. }
  97. byte[] bsSrcString = Encoding.Default.GetBytes(strStr);
  98. if (bsSrcString.Length <= strLength)
  99. {
  100. return strMyStr;
  101. }
  102. int nRealLength = strLength;
  103. int[] anResultFlag = new int[strLength];
  104. byte[] bsResult = null;
  105. int nFlag = 0;
  106. for (int i = 0; i < strLength; i++)
  107. {
  108. if (bsSrcString[i] > 0x7f)
  109. {
  110. nFlag++;
  111. if (nFlag == 3)
  112. {
  113. nFlag = 1;
  114. }
  115. }
  116. else
  117. {
  118. nFlag = 0;
  119. }
  120. anResultFlag[i] = nFlag;
  121. }
  122. if ((bsSrcString[strLength - 1] > 0x7f) && (anResultFlag[strLength - 1] == 1))
  123. {
  124. nRealLength = strLength + 1;
  125. }
  126. bsResult = new byte[nRealLength];
  127. Array.Copy(bsSrcString, bsResult, nRealLength);
  128. return (Encoding.Default.GetString(bsResult) + strReplace);
  129. }
  130. #endregion
  131. #region 替换html字符
  132. /// <summary>
  133. /// 替换html字符
  134. /// </summary>
  135. public static string EncodeHtml(string strHtml)
  136. {
  137. if (strHtml != "")
  138. {
  139. strHtml = strHtml.Replace(",", "&def");
  140. strHtml = strHtml.Replace("'", "&dot");
  141. strHtml = strHtml.Replace(";", "&dec");
  142. return strHtml;
  143. }
  144. return "";
  145. }
  146. #endregion
  147. #region 生成指定位数的随机数
  148. public static string GenerateRandNum(int Length, RandEnum randtype)
  149. {
  150. string code = "";
  151. Random Rnd = new Random();
  152. if (code.Length < Length)
  153. {
  154. switch (randtype)
  155. {
  156. case RandEnum.Int:
  157. for (int i = 0; i < Length - code.Length; i++)
  158. {
  159. code = code + Rnd.Next(0, 9).ToString();
  160. }
  161. break;
  162. case RandEnum.DateTime:
  163. code = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
  164. for (int i = 0; i < Length - code.Length; i++)
  165. {
  166. code = code + Rnd.Next(0, 9).ToString();
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. return code;
  174. }
  175. public enum RandEnum
  176. {
  177. Int,
  178. DateTime
  179. }
  180. #endregion
  181. #region 取页面Get传递的参数
  182. /// <summary>
  183. /// 取URL传递的参数
  184. /// </summary>
  185. /// <param name="name"></param>
  186. /// <returns></returns>
  187. public static string Get(string name)
  188. {
  189. string text1 = HttpContext.Current.Request.QueryString[name];
  190. return ((text1 == null) ? string.Empty : text1.Trim());
  191. }
  192. /// <summary>
  193. /// 取URL传递的参数
  194. /// </summary>
  195. /// <param name="name">指定参数名</param>
  196. /// <param name="chkType">类型</param>
  197. /// <returns></returns>
  198. public static string Get(string name, CheckGetEnum chkType)
  199. {
  200. string text1 = Get(name);
  201. bool flag1 = false;
  202. switch (chkType)
  203. {
  204. case CheckGetEnum.Int:
  205. flag1 = RegExp.IsNumber(text1);
  206. break;
  207. case CheckGetEnum.Safety:
  208. flag1 = RegExp.IsSafety(text1);
  209. break;
  210. case CheckGetEnum.Json:
  211. flag1 = RegExp.IsJsonSafety(text1);
  212. break;
  213. default:
  214. flag1 = true;
  215. break;
  216. }
  217. if (!flag1)
  218. {
  219. throw new Exception("hc1305");
  220. }
  221. return text1;
  222. }
  223. #endregion
  224. #region 取提交Post页面指定值
  225. /// <summary>
  226. /// 取提交页面指定值
  227. /// </summary>
  228. /// <param name="name"></param>
  229. /// <returns></returns>
  230. public static string Post(string name)
  231. {
  232. string text1 = HttpContext.Current.Request.Form[name];
  233. return ((text1 == null) ? string.Empty : text1.Trim());
  234. }
  235. /// <summary>
  236. /// 取提交页面指定值
  237. /// </summary>
  238. /// <param name="name"></param>
  239. /// <param name="chkType">类型</param>
  240. /// <returns></returns>
  241. public static string Post(string name, CheckGetEnum chkType)
  242. {
  243. string text1 = HttpContext.Current.Request.Form[name];
  244. if (text1 == null)
  245. {
  246. return string.Empty;
  247. }
  248. bool flag1 = false;
  249. switch (chkType)
  250. {
  251. case CheckGetEnum.Int:
  252. flag1 = RegExp.IsNumber(text1);
  253. break;
  254. case CheckGetEnum.Safety:
  255. flag1 = RegExp.IsSafety(text1);
  256. break;
  257. case CheckGetEnum.Json:
  258. flag1 = RegExp.IsJsonSafety(text1);
  259. break;
  260. default:
  261. flag1 = true;
  262. break;
  263. }
  264. if (!flag1)
  265. {
  266. throw new Exception("hc1305");
  267. }
  268. return text1.Trim();
  269. }
  270. /// <summary>
  271. /// 枚举
  272. /// </summary>
  273. public enum CheckGetEnum
  274. {
  275. Int,
  276. Safety,
  277. Json
  278. }
  279. #endregion
  280. #region 返回标准日期格式string
  281. /// <summary>
  282. /// 返回标准日期格式string
  283. /// </summary>
  284. public static string GetDate()
  285. {
  286. return DateTime.Now.ToString("yyyy-MM-dd");
  287. }
  288. #endregion
  289. #region 返回指定日期格式
  290. /// <summary>
  291. /// 返回指定日期格式
  292. /// </summary>
  293. public static string GetDate(string datetimestr, string replacestr)
  294. {
  295. if (datetimestr == null)
  296. {
  297. return replacestr;
  298. }
  299. if (datetimestr.Equals(""))
  300. {
  301. return replacestr;
  302. }
  303. try
  304. {
  305. datetimestr = Convert.ToDateTime(datetimestr).ToString("yyyy-MM-dd").Replace("1900-01-01", replacestr);
  306. }
  307. catch
  308. {
  309. return replacestr;
  310. }
  311. return datetimestr;
  312. }
  313. #endregion
  314. #region 返回标准时间格式string
  315. /// <summary>
  316. /// 返回标准时间格式string:yyyy-MM-dd HH:mm:ss
  317. /// </summary>
  318. public static string GetDateTime()
  319. {
  320. return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  321. }
  322. #endregion
  323. #region 获取文件内容
  324. /// <summary>
  325. /// 获取文件内容
  326. /// </summary>
  327. /// <param name="strPath">文件路径</param>
  328. /// <returns>文件内容</returns>
  329. public static string GetFileContent(string strPath)
  330. {
  331. StreamReader fileStream = new StreamReader(strPath, Encoding.Default);
  332. string ct = fileStream.ReadToEnd();
  333. fileStream.Close();
  334. return ct;
  335. }
  336. #endregion
  337. #region 获得当前绝对路径
  338. /// <summary>
  339. /// 获得当前绝对路径
  340. /// </summary>
  341. /// <param name="strPath">指定的路径</param>
  342. /// <returns>绝对路径</returns>
  343. public static string GetSitePath(string strPath)
  344. {
  345. if (HttpContext.Current != null)
  346. {
  347. return HttpContext.Current.Server.MapPath(strPath);
  348. }
  349. return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
  350. }
  351. #endregion
  352. #region 获取字符串长度,注意,1个汉字长度为2
  353. /// <summary>
  354. /// 获取字符串长度,注意,1个汉字长度为2
  355. /// </summary>
  356. /// <param name="str">要检查字符串</param>
  357. /// <returns>字符串长度</returns>
  358. public static int GetStringLength(string str)
  359. {
  360. return Encoding.Default.GetBytes(str).Length;
  361. }
  362. #endregion
  363. #region MD5函数
  364. /// <summary>
  365. /// MD5函数
  366. /// </summary>
  367. /// <param name="str">原始字符串</param>
  368. /// <returns>MD5结果</returns>
  369. public static string MD5(string str)
  370. {
  371. byte[] b = Encoding.Default.GetBytes(str);
  372. b = new MD5CryptoServiceProvider().ComputeHash(b);
  373. string ret = "";
  374. for (int i = 0; i < b.Length; i++)
  375. {
  376. ret = ret + b[i].ToString("x").PadLeft(2, '0');
  377. }
  378. return ret;
  379. }
  380. #endregion
  381. #region 移除Html标记
  382. /// <summary>
  383. /// 移除Html标记
  384. /// </summary>
  385. /// <param name="content"></param>
  386. /// <returns></returns>
  387. public static string RemoveHtml(string content)
  388. {
  389. string regexstr = "<[^>]*>";
  390. return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
  391. }
  392. #endregion
  393. #region 删除字符串尾部的回车/换行/空格
  394. /// <summary>
  395. /// 删除字符串尾部的回车/换行/空格
  396. /// </summary>
  397. /// <param name="str"></param>
  398. /// <returns></returns>
  399. public static string RTrim(string str)
  400. {
  401. for (int i = str.Length; i >= 0; i--)
  402. {
  403. char ts = str[i];
  404. if (!ts.Equals(" "))
  405. {
  406. ts = str[i];
  407. }
  408. if (ts.Equals("\r") || (ts = str[i]).Equals("\n"))
  409. {
  410. str.Remove(i, 1);
  411. }
  412. }
  413. return str;
  414. }
  415. #endregion
  416. #region SHA256函数
  417. /// <summary>
  418. /// SHA256函数
  419. /// </summary>
  420. /// /// <param name="str">原始字符串</param>
  421. /// <returns>SHA256结果</returns>
  422. public static string SHA256(string str)
  423. {
  424. byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
  425. SHA256Managed Sha256 = new SHA256Managed();
  426. byte[] Result = Sha256.ComputeHash(SHA256Data);
  427. return Convert.ToBase64String(Result); //返回长度为44字节的字符串
  428. }
  429. #endregion
  430. #region 生成指定数量的html空格符号
  431. /// <summary>
  432. /// 生成指定数量的html空格符号
  433. /// </summary>
  434. public static string Spaces(int nSpaces)
  435. {
  436. StringBuilder sb = new StringBuilder();
  437. for (int i = 0; i < nSpaces; i++)
  438. {
  439. sb.Append(" &nbsp;&nbsp;");
  440. }
  441. return sb.ToString();
  442. }
  443. #endregion
  444. #region 生成指定数量的html符号
  445. /// <summary>
  446. /// 生成指定数量的html符号
  447. /// </summary>
  448. public static string Spaces(int nSpaces, string strHtml)
  449. {
  450. StringBuilder sb = new StringBuilder();
  451. for (int i = 0; i < nSpaces; i++)
  452. {
  453. sb.Append(strHtml);
  454. }
  455. return sb.ToString();
  456. }
  457. #endregion
  458. #region 替换回车换行符为html换行符
  459. /// <summary>
  460. /// 替换回车换行符为html换行符
  461. /// </summary>
  462. public static string StrFormat(string str)
  463. {
  464. string str2;
  465. if (str == null)
  466. {
  467. str2 = "";
  468. }
  469. else
  470. {
  471. str = str.Replace("\r\n", "<br />");
  472. str = str.Replace("\n", "<br />");
  473. str2 = str;
  474. }
  475. return str2;
  476. }
  477. #endregion
  478. #region string型转换为int型
  479. /// <summary>
  480. /// string型转换为int型
  481. /// </summary>
  482. /// <param name="strValue">要转换的字符串</param>
  483. /// <param name="defValue">缺省值</param>
  484. /// <returns>转换后的int类型结果</returns>
  485. public static int StrToInt(object strValue, int defValue)
  486. {
  487. if ((strValue == null) || (strValue.ToString() == string.Empty) || (strValue.ToString().Length > 10))
  488. {
  489. return defValue;
  490. }
  491. string val = strValue.ToString();
  492. string firstletter = val[0].ToString();
  493. if (val.Length == 10 && RegExp.IsNumber(firstletter) && int.Parse(firstletter) > 1)
  494. {
  495. return defValue;
  496. }
  497. else if (val.Length == 10 && !RegExp.IsNumber(firstletter))
  498. {
  499. return defValue;
  500. }
  501. int intValue = defValue;
  502. if (strValue != null)
  503. {
  504. bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString());
  505. if (IsInt)
  506. {
  507. intValue = Convert.ToInt32(strValue);
  508. }
  509. }
  510. return intValue;
  511. }
  512. #endregion
  513. #region 返回 URL 字符串的编码结果
  514. /// <summary>
  515. /// 返回 URL 字符串的编码结果
  516. /// </summary>
  517. /// <param name="str">字符串</param>
  518. /// <returns>解码结果</returns>
  519. public static string UrlDecode(string str)
  520. {
  521. return HttpUtility.UrlDecode(str);
  522. }
  523. #endregion
  524. #region 返回 HTML 字符串的编码结果
  525. /// <summary>
  526. /// 返回 HTML 字符串的编码结果
  527. /// </summary>
  528. /// <param name="str">字符串</param>
  529. /// <returns>编码结果</returns>
  530. public static string HtmlEncode(string str)
  531. {
  532. return HttpUtility.HtmlEncode(str);
  533. }
  534. #endregion
  535. #region 返回 HTML 字符串的解码结果
  536. /// <summary>
  537. /// 返回 HTML 字符串的解码结果
  538. /// </summary>
  539. /// <param name="str">字符串</param>
  540. /// <returns>解码结果</returns>
  541. public static string HtmlDecode(string str)
  542. {
  543. return HttpUtility.HtmlDecode(str);
  544. }
  545. #endregion
  546. }
  547. }