StringHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using System.Web.UI.WebControls;
  8. using System.Security.Cryptography;
  9. using System.Diagnostics;
  10. using System.Web;
  11. using System.Collections;
  12. namespace Ant.Common
  13. {
  14. public class StringHelper
  15. {
  16. #region 删除HTML标记
  17. /// <summary>
  18. /// 删除HTML标记
  19. /// </summary>
  20. /// <param name="htmlString">带有样式的字符串</param>
  21. /// <returns></returns>
  22. public static string RemoveHtmlFormat(string htmlString)
  23. {
  24. return Regex.Replace(htmlString, "<[^>]+>", "");
  25. }
  26. #endregion
  27. #region 截断字符串
  28. /// <summary>
  29. /// 截断字符串
  30. /// </summary>
  31. /// <param name="str">要截断的字符串</param>
  32. /// <param name="length">长度</param>
  33. /// <returns></returns>
  34. public static string CutString(string str, int length)
  35. {
  36. int i = 0, j = 0;
  37. foreach (char chr in str)
  38. {
  39. i += 2;
  40. if (i > length)
  41. {
  42. str = str.Substring(0, j - 1) + "...";
  43. break;
  44. }
  45. j++;
  46. }
  47. return str;
  48. }
  49. #endregion
  50. #region 生成唯一ID 由数字组成
  51. /// <summary>
  52. /// 生成唯一ID
  53. /// </summary>
  54. /// <returns></returns>
  55. public static string CreateIDCode()
  56. {
  57. DateTime Time1 = DateTime.Now.ToUniversalTime();
  58. DateTime Time2 = Convert.ToDateTime("1970-01-01");
  59. TimeSpan span = Time1 - Time2; //span就是两个日期之间的差额
  60. string t = span.TotalMilliseconds.ToString("0");
  61. return t;
  62. }
  63. #endregion
  64. #region 压缩与解压字符串
  65. #region 压缩字符串
  66. /// <summary>
  67. /// 压缩字符串
  68. /// </summary>
  69. /// <param name="unCompressedString">要压缩的字符串</param>
  70. /// <returns></returns>
  71. public static string ZipString(string unCompressedString)
  72. {
  73. byte[] bytData = System.Text.Encoding.UTF8.GetBytes(unCompressedString);
  74. MemoryStream ms = new MemoryStream();
  75. Stream s = new GZipStream(ms, CompressionMode.Compress);
  76. s.Write(bytData, 0, bytData.Length);
  77. s.Close();
  78. byte[] compressedData = (byte[])ms.ToArray();
  79. return System.Convert.ToBase64String(compressedData, 0, compressedData.Length);
  80. }
  81. #endregion
  82. #region 解压字符串
  83. /// <summary>
  84. /// 解压字符串
  85. /// </summary>
  86. /// <param name="unCompressedString">要解压的字符串</param>
  87. /// <returns></returns>
  88. public static string UnzipString(string unCompressedString)
  89. {
  90. System.Text.StringBuilder uncompressedString = new System.Text.StringBuilder();
  91. byte[] writeData = new byte[4096];
  92. byte[] bytData = System.Convert.FromBase64String(unCompressedString);
  93. int totalLength = 0;
  94. int size = 0;
  95. Stream s = new GZipStream(new MemoryStream(bytData), CompressionMode.Decompress);
  96. while (true)
  97. {
  98. size = s.Read(writeData, 0, writeData.Length);
  99. if (size > 0)
  100. {
  101. totalLength += size;
  102. uncompressedString.Append(System.Text.Encoding.UTF8.GetString(writeData, 0, size));
  103. }
  104. else
  105. {
  106. break;
  107. }
  108. }
  109. s.Close();
  110. return uncompressedString.ToString();
  111. }
  112. #endregion
  113. #endregion
  114. #region 转全角的函数(SBC case)
  115. ///
  116. /// 转全角的函数(SBC case)
  117. ///
  118. /// 任意字符串
  119. /// 全角字符串
  120. ///
  121. ///全角空格为12288,半角空格为32///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  122. ///
  123. public string ToSBC(string input)
  124. {
  125. //半角转全角:
  126. char[] c = input.ToCharArray();
  127. for (int i = 0; i < c.Length; i++)
  128. {
  129. if (c[i] == 32)
  130. {
  131. c[i] = (char)12288;
  132. continue;
  133. }
  134. if (c[i] < 127)
  135. c[i] = (char)(c[i] + 65248);
  136. }
  137. return new string(c);
  138. }
  139. #endregion
  140. #region 转半角的函数(DBC case)
  141. ///
  142. /// 转半角的函数(DBC case)
  143. ///
  144. /// 任意字符串
  145. /// 半角字符串
  146. ///
  147. ///全角空格为12288,半角空格为32
  148. ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  149. ///
  150. public string ToDBC(string input)
  151. {
  152. char[] c = input.ToCharArray();
  153. for (int i = 0; i < c.Length; i++)
  154. {
  155. if (c[i] == 12288)
  156. {
  157. c[i] = (char)32;
  158. continue;
  159. }
  160. if (c[i] > 65280 && c[i] < 65375)
  161. c[i] = (char)(c[i] - 65248);
  162. }
  163. return new string(c);
  164. }
  165. #endregion
  166. #region Html 编码
  167. /// <summary>
  168. /// 对文本框中的字符进行HTML编码
  169. /// </summary>
  170. /// <param name="str">源字符串</param>
  171. /// <returns></returns>
  172. public static string HtmlEncode(string str)
  173. {
  174. str = str.Replace("&", "&");
  175. str = str.Replace("'", "''");
  176. str = str.Replace("\"", "\"");
  177. str = str.Replace(" ", " ");
  178. str = str.Replace("<", "<");
  179. str = str.Replace(">", ">");
  180. str = str.Replace("\n", "<br>");
  181. return str;
  182. }
  183. /// <summary>
  184. /// 对字符串进行HTML解码,解析为可为页面识别的代码
  185. /// </summary>
  186. /// <param name="str">要解码的字符串</param>
  187. /// <returns></returns>
  188. public static string HtmlDecode(string str)
  189. {
  190. str = str.Replace("<br>", "\n");
  191. str = str.Replace(">", ">");
  192. str = str.Replace("<", "<");
  193. str = str.Replace(" ", " ");
  194. str = str.Replace("\"", "'");
  195. return str;
  196. }
  197. #endregion
  198. #region 用户名过滤
  199. /// <summary>
  200. ///
  201. /// </summary>
  202. /// <param name="userName"></param>
  203. /// <returns></returns>
  204. public static bool Filter(string userName)
  205. {
  206. if (IsExist(userName, "!")) return false;
  207. if (IsExist(userName, "!")) return false;
  208. if (IsExist(userName, "#")) return false;
  209. if (IsExist(userName, "&")) return false;
  210. if (IsExist(userName, "$")) return false;
  211. if (IsExist(userName, "*")) return false;
  212. if (IsExist(userName, ".")) return false;
  213. if (IsExist(userName, ",")) return false;
  214. if (IsExist(userName, ";")) return false;
  215. if (IsExist(userName, "'")) return false;
  216. if (IsExist(userName, "<")) return false;
  217. if (IsExist(userName, ">")) return false;
  218. return true;
  219. }
  220. public static bool IsExist(string userName, string filterStr)
  221. {
  222. if (userName.IndexOf(filterStr) > -1)
  223. return true;
  224. return false;
  225. }
  226. #endregion
  227. #region SQL注入过滤
  228. /// <summary>
  229. ///SQL注入过滤
  230. /// </summary>
  231. /// <param name="InText">要进行过滤的字符串</param>
  232. /// <returns>如果参数存在不安全字符,则返回true</returns>
  233. public static bool SqlFilter2(string InText)
  234. {
  235. string word = "exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join";
  236. if (InText == null)
  237. return false;
  238. foreach (string i in word.Split('|'))
  239. {
  240. if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
  241. {
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. /// <summary>
  248. /// 将指定的str串执行sql关键字过滤并返回
  249. /// </summary>
  250. /// <param name="str">要过滤的字符串</param>
  251. /// <returns></returns>
  252. public static string SqlFilter(string str)
  253. {
  254. return str.Replace("'", "").Replace("'", "").Replace("--", "").Replace("&", "").Replace("/*", "").Replace(";", "").Replace("%", "");
  255. }
  256. /// <summary>
  257. /// 将指定的串列表执行sql关键字过滤并以[,]号分隔返回
  258. /// </summary>
  259. /// <param name="strs"></param>
  260. /// <returns></returns>
  261. public static string SqlFilters(params string[] strs)
  262. {
  263. StringBuilder sb = new StringBuilder();
  264. foreach (string str in strs)
  265. {
  266. sb.Append(SqlFilter(str) + ",");
  267. }
  268. if (sb.Length > 0)
  269. return sb.ToString().TrimEnd(',');
  270. return "";
  271. }
  272. public static bool ProcessSqlStr(string Str)
  273. {
  274. bool ReturnValue = false;
  275. try
  276. {
  277. if (Str != "")
  278. {
  279. string SqlStr = "'|insert|select*|and'|or'|insertinto|deletefrom|altertable|update|createtable|createview|dropview|createindex|dropindex|createprocedure|dropprocedure|createtrigger|droptrigger|createschema|dropschema|createdomain|alterdomain|dropdomain|);|select@|declare@|print@|char(|select";
  280. string[] anySqlStr = SqlStr.Split('|');
  281. foreach (string ss in anySqlStr)
  282. {
  283. if (Str.IndexOf(ss) >= 0)
  284. {
  285. ReturnValue = true;
  286. }
  287. }
  288. }
  289. }
  290. catch
  291. {
  292. ReturnValue = true;
  293. }
  294. return ReturnValue;
  295. }
  296. #endregion
  297. #region 获取CheckBoxList控件中选中的项
  298. /// <summary>
  299. /// 获取CheckBoxList控件中选中的项的value,字符串由,分隔
  300. /// </summary>
  301. /// <param name="chk">CheckBoxList 控件ID</param>
  302. /// <returns></returns>
  303. public static string GetCheckedItemValue(CheckBoxList chk)
  304. {
  305. string s = "";
  306. foreach (ListItem li in chk.Items)
  307. {
  308. if (li.Selected)
  309. s += li.Value + ",";
  310. }
  311. return s.TrimEnd(',');
  312. }
  313. /// <summary>
  314. /// 获取CheckBoxList控件中选中的项的Text,字符串由,分隔
  315. /// </summary>
  316. /// <param name="chk">CheckBoxList 控件ID</param>
  317. /// <returns></returns>
  318. public static string GetCheckedItemText(CheckBoxList chk)
  319. {
  320. string s = "";
  321. foreach (ListItem li in chk.Items)
  322. {
  323. if (li.Selected)
  324. s += li.Text + ",";
  325. }
  326. return s.TrimEnd(',');
  327. }
  328. #endregion
  329. #region 根据提供的Id字符串,将列表中的项选中
  330. /// <summary>
  331. /// 根据提供的Id字符串,将列表中的项选中
  332. /// </summary>
  333. /// <param name="itemid">Id字符串,由,分隔</param>
  334. /// <param name="checkboxlist">CheckBoxList控件</param>
  335. public static void CheckItem(string itemid, CheckBoxList checkboxlist)
  336. {
  337. foreach (ListItem li in checkboxlist.Items)
  338. {
  339. if (itemid.IndexOf(li.Value) != -1)
  340. li.Selected = true;
  341. }
  342. }
  343. #endregion
  344. #region 加密字符串 MD5
  345. #region 利用 MD5 加密算法加密字符串
  346. /// <summary>
  347. /// 利用 MD5 加密算法加密字符串
  348. /// </summary>
  349. /// <param name="src">字符串源串</param>
  350. /// <returns>返加MD5 加密后的字符串</returns>
  351. public static string ComputeMD5(string src)
  352. {
  353. //将密码字符串转化成字节数组
  354. byte[] byteArray = GetByteArray(src);
  355. //计算 MD5 密码
  356. byteArray = (new MD5CryptoServiceProvider().ComputeHash(byteArray));
  357. //将字节码转化成字符串并返回
  358. return BitConverter.ToString(byteArray);
  359. }
  360. /// <summary>
  361. /// 将指定串加密为不包含中杠的MD5值
  362. /// </summary>
  363. /// <param name="str">要加密的字符串</param>
  364. /// <param name="isupper">返回值的大小写(true大写,false小写)</param>
  365. /// <returns></returns>
  366. public static string ComputeMD5(string str, bool isupper)
  367. {
  368. string md5str = ComputeMD5(str);
  369. if (isupper)
  370. return md5str.ToUpper();
  371. return md5str.ToLower();
  372. }
  373. #endregion
  374. #region 将字符串翻译成字节数组
  375. /// <summary>
  376. /// 将字符串翻译成字节数组
  377. /// </summary>
  378. /// <param name="src">字符串源串</param>
  379. /// <returns>字节数组</returns>
  380. private static byte[] GetByteArray(string src)
  381. {
  382. byte[] byteArray = new byte[src.Length];
  383. for (int i = 0; i < src.Length; i++)
  384. {
  385. byteArray[i] = Convert.ToByte(src[i]);
  386. }
  387. return byteArray;
  388. }
  389. #endregion
  390. #region MD5string
  391. public static string MD5string(string str)
  392. {
  393. return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
  394. }
  395. public static string MD5string(string str, bool isupper)
  396. {
  397. string md5string = MD5string(str);
  398. if (isupper)
  399. return md5string.ToUpper();
  400. else
  401. return md5string.ToLower();
  402. }
  403. #endregion
  404. #endregion
  405. #region SHA1 加密
  406. public string SHA1(string Source_String)
  407. {
  408. byte[] StrRes = Encoding.Default.GetBytes(Source_String);
  409. HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
  410. StrRes = iSHA.ComputeHash(StrRes);
  411. StringBuilder EnText = new StringBuilder();
  412. foreach (byte iByte in StrRes)
  413. {
  414. EnText.AppendFormat("{0:x2}", iByte);
  415. }
  416. return EnText.ToString();
  417. }
  418. #endregion
  419. #region DES加密字符串
  420. /// <summary>
  421. /// DES加密字符串
  422. /// </summary>
  423. /// <param name="encryptString">待加密的字符串</param>
  424. /// <param name="encryptKey">加密密钥,要求为8位</param>
  425. /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
  426. public static string EncryptDES(string encryptString, string key)
  427. {
  428. try
  429. {
  430. byte[] rgbKey = Encoding.UTF8.GetBytes(key);
  431. byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  432. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  433. DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
  434. MemoryStream mStream = new MemoryStream();
  435. CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  436. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  437. cStream.FlushFinalBlock();
  438. return Convert.ToBase64String(mStream.ToArray());
  439. }
  440. catch
  441. {
  442. return encryptString;
  443. }
  444. }
  445. #endregion
  446. #region DES解密字符串
  447. /// <summary>
  448. /// DES解密字符串
  449. /// </summary>
  450. /// <param name="decryptString">待解密的字符串</param>
  451. /// <param name="key">解密密钥,要求8位</param>
  452. /// <returns></returns>
  453. public static string DecryptDES(string decryptString, string key)
  454. {
  455. try
  456. {
  457. //默认密钥向量
  458. byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  459. byte[] rgbKey = Encoding.UTF8.GetBytes(key);
  460. byte[] rgbIV = Keys;
  461. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  462. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  463. MemoryStream mStream = new MemoryStream();
  464. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  465. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  466. cStream.FlushFinalBlock();
  467. return Encoding.UTF8.GetString(mStream.ToArray());
  468. }
  469. catch
  470. {
  471. return decryptString;
  472. }
  473. }
  474. #endregion
  475. #region AES 加密 解密
  476. /// <summary>
  477. /// AES加密
  478. /// </summary>
  479. /// <param name="str">要加密字符串</param>
  480. /// <returns>返回加密后字符串</returns>
  481. public static String EncryptAES(String str, string aeskey)
  482. {
  483. Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(aeskey);
  484. Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);
  485. System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
  486. rDel.Key = keyArray;
  487. rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
  488. rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
  489. System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
  490. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  491. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  492. }
  493. /// <summary>
  494. /// AES解密
  495. /// </summary>
  496. /// <param name="str">要解密字符串</param>
  497. /// <returns>返回解密后字符串</returns>
  498. public static String DecryptAES(String str, string aeskey)
  499. {
  500. Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(aeskey);
  501. Byte[] toEncryptArray = Convert.FromBase64String(str);
  502. System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
  503. rDel.Key = keyArray;
  504. rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
  505. rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
  506. System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
  507. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  508. return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
  509. }
  510. #endregion
  511. #region base64 字符串编码
  512. /// <summary>
  513. /// base64 字符串编码
  514. /// </summary>
  515. /// <param name="str">要编码的字符串</param>
  516. /// <returns></returns>
  517. public static string ToBase64(string str)
  518. {
  519. byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
  520. return Convert.ToBase64String(data);
  521. }
  522. /// <summary>
  523. /// base 64 字符串解码
  524. /// </summary>
  525. /// <param name="base64str">要解码的字符串</param>
  526. /// <returns></returns>
  527. public static string UnBase64(string base64str)
  528. {
  529. byte[] data = Convert.FromBase64String(base64str);
  530. return System.Text.ASCIIEncoding.ASCII.GetString(data);
  531. }
  532. #endregion
  533. #region 转换为中文星期
  534. /// <summary>
  535. /// 转换为中文星期
  536. /// </summary>
  537. /// <param name="dayfweek">英文星期</param>
  538. /// <returns></returns>
  539. public static string ConvertWeekDayToCn(DayOfWeek dayfweek)
  540. {
  541. switch (dayfweek)
  542. {
  543. case DayOfWeek.Sunday:
  544. return "星期日";
  545. case DayOfWeek.Monday:
  546. return "星期一";
  547. case DayOfWeek.Tuesday:
  548. return "星期二";
  549. case DayOfWeek.Wednesday:
  550. return "星期三";
  551. case DayOfWeek.Thursday:
  552. return "星期四";
  553. case DayOfWeek.Friday:
  554. return "星期五";
  555. case DayOfWeek.Saturday:
  556. return "星期六";
  557. default:
  558. return "";
  559. }
  560. }
  561. #endregion
  562. #region 执行CMD 命令
  563. /// <summary>
  564. /// 执行CMD 命令
  565. /// </summary>
  566. /// <param name="strCommand">命令串</param>
  567. /// <returns></returns>
  568. public static string RunCommand(string strCommand)
  569. {
  570. Process process = new Process();
  571. process.StartInfo.FileName = "CMD.exe";
  572. process.StartInfo.UseShellExecute = false;
  573. process.StartInfo.RedirectStandardInput = true;
  574. process.StartInfo.RedirectStandardOutput = true;
  575. process.StartInfo.RedirectStandardError = true;
  576. process.StartInfo.CreateNoWindow = true;
  577. process.Start();
  578. process.StandardInput.WriteLine(strCommand);
  579. process.StandardInput.WriteLine("exit");
  580. string str = process.StandardOutput.ReadToEnd();
  581. process.Close();
  582. return str;
  583. }
  584. #endregion
  585. public static string Escape(string s)
  586. {
  587. StringBuilder builder = new StringBuilder();
  588. byte[] bytes = Encoding.Unicode.GetBytes(s);
  589. for (int i = 0; i < bytes.Length; i += 2)
  590. {
  591. builder.Append("%u");
  592. builder.Append(bytes[i + 1].ToString("X2"));
  593. builder.Append(bytes[i].ToString("X2"));
  594. }
  595. return builder.ToString();
  596. }
  597. public static string ReplaceHtml(string str)
  598. {
  599. if (str == null || str.Length == 0)
  600. return "";
  601. str = str.Replace("<", "<");
  602. str = str.Replace(">", ">");
  603. str = str.Replace("\n", "");
  604. str = str.Replace("\r", "");
  605. return str;
  606. }
  607. public static string ReplaceEnter(string str)
  608. {
  609. if (str == null || str.Length == 0)
  610. return "";
  611. str = str.Replace("\n", "");
  612. str = str.Replace("\r", "");
  613. return str;
  614. }
  615. /// <summary>
  616. /// 替换指定符串中的首个指定字符为新的字符
  617. /// </summary>
  618. /// <param name="sourcestr">要修改的字符串</param>
  619. /// <param name="oldstr">被替换的字符串</param>
  620. /// <param name="newstr">替换字符串 </param>
  621. /// <returns></returns>
  622. public static string ReplaceFirst(string sourcestr, string oldstr, string newstr)
  623. {
  624. Regex reg = new Regex(oldstr);
  625. if (reg.IsMatch(sourcestr))
  626. {
  627. sourcestr = reg.Replace(sourcestr, newstr, 1);
  628. }
  629. return sourcestr;
  630. }
  631. #region 生成随机字符串,格式:1q2w3e4r
  632. /// <summary>
  633. /// 生成随机字符串,格式:1q2w3e4r
  634. /// </summary>
  635. /// <returns></returns>
  636. public static string BuildPassword()
  637. {
  638. Random random = new Random();
  639. List<int> ints = new List<int>();
  640. for (int i = 0; i < 4; i++)
  641. {
  642. ints.Add(random.Next(9));
  643. }
  644. List<string> strs = new List<string>();
  645. //string CodeSerial = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
  646. string CodeSerial = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z";
  647. string[] arr = CodeSerial.Split(',');
  648. int randValue = -1;
  649. Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
  650. for (int i = 0; i < 4; i++)
  651. {
  652. randValue = rand.Next(0, arr.Length - 1);
  653. strs.Add(arr[randValue]);
  654. }
  655. string passwd = "";
  656. for (int k = 0; k < 4; k++)
  657. {
  658. passwd += ints[k].ToString() + strs[k];
  659. }
  660. return passwd;
  661. }
  662. #endregion
  663. public static object GetRequestObject(string key)
  664. {
  665. return HttpContext.Current.Request[key];
  666. }
  667. }
  668. }