Command_StringPlus.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*******************************************************************************
  2. * iNethinkCMS - 网站内容管理系统
  3. * Copyright (C) 2012-2013 inethink.com
  4. *
  5. * @author jackyang <69991000@qq.com>
  6. * @website http://cms.inethink.com
  7. * @version 1.3.6.0 (2013-08-14)
  8. *
  9. * This is licensed under the GNU LGPL, version 3.0 or later.
  10. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
  11. *******************************************************************************/
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. namespace iNethinkCMS.Command
  17. {
  18. public class Command_StringPlus
  19. {
  20. ///
  21. /// left截取字符串
  22. ///
  23. public static string Left(string sSource, int iLength)
  24. {
  25. return sSource.Substring(0, iLength > sSource.Length ? sSource.Length : iLength);
  26. }
  27. ///
  28. /// Right截取字符串
  29. ///
  30. public static string Right(string sSource, int iLength)
  31. {
  32. return sSource.Substring(iLength > sSource.Length ? 0 : sSource.Length - iLength);
  33. }
  34. ///
  35. /// mid截取字符串
  36. ///
  37. public static string Mid(string sSource, int iStart, int iLength)
  38. {
  39. int iStartPoint = iStart > sSource.Length ? sSource.Length : iStart;
  40. return sSource.Substring(iStartPoint, iStartPoint + iLength > sSource.Length ? sSource.Length - iStartPoint : iLength);
  41. }
  42. /// <summary>
  43. /// 过滤字符串中的html代码
  44. /// </summary>
  45. /// <param name="Str"></param>
  46. /// <returns>返回过滤之后的字符串</returns>
  47. public static string LostHTML(string Str)
  48. {
  49. string Re_Str = "";
  50. if (Str != null)
  51. {
  52. if (Str != string.Empty)
  53. {
  54. string Pattern = "<\\/*[^<>]*>";
  55. Re_Str = Regex.Replace(Str, Pattern, "");
  56. }
  57. }
  58. return (Re_Str.Replace("\\r\\n", "")).Replace("\\r", "").Replace("&nbsp;", "");
  59. }
  60. /// <summary>
  61. /// 获得日期的间隔TimeSpan
  62. /// </summary>
  63. /// <param name="DateTime1">日期一。</param>
  64. /// <param name="DateTime2">日期二</param>
  65. /// <returns>TimeSpan。</returns>
  66. public static TimeSpan DateDiff(DateTime DateTime_One, DateTime DateTime_Two)
  67. {
  68. TimeSpan ts1 = new TimeSpan(DateTime_One.Ticks);
  69. TimeSpan ts2 = new TimeSpan(DateTime_Two.Ticks);
  70. TimeSpan ts = ts1.Subtract(ts2).Duration();
  71. return ts;
  72. }
  73. /// <summary>
  74. /// 从字符串里随机得到,规定个数的字符串.
  75. /// </summary>
  76. /// <param name="allChar"></param>
  77. /// <param name="CodeCount"></param>
  78. /// <param name="tSleep">是否要在生成前将当前线程阻止以避免重复</param>
  79. /// <returns></returns>
  80. public static string RandomCode(string byChar, int CodeCount)
  81. {
  82. return RandomCode(byChar, CodeCount, false);
  83. }
  84. public static string RandomCode(string byChar, int CodeCount, bool tSleep)
  85. {
  86. string randomChar = "";
  87. switch (byChar)
  88. {
  89. case "num":
  90. randomChar = "1,2,3,4,5,6,7,8,9";
  91. break;
  92. case "maxen":
  93. randomChar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  94. break;
  95. case "minien":
  96. randomChar = "a,b,c,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
  97. break;
  98. case "minien&num":
  99. randomChar = "1,2,3,4,5,6,7,8,9,a,b,c,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
  100. break;
  101. case "all":
  102. randomChar = "1,2,3,4,5,6,7,8,9,a,b,c,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"
  103. + "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  104. break;
  105. case "verificationcode":
  106. randomChar = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
  107. break;
  108. default:
  109. randomChar = byChar;
  110. break;
  111. }
  112. randomChar = randomChar.Replace(",","");
  113. string RandomCode = "";
  114. if (tSleep)
  115. {
  116. System.Threading.Thread.Sleep(3);
  117. }
  118. char[] allCharArray = randomChar.ToCharArray();
  119. int n = allCharArray.Length;
  120. System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
  121. for (int i = 0; i < CodeCount; i++)
  122. {
  123. int rnd = random.Next(0, n);
  124. RandomCode += allCharArray[rnd];
  125. }
  126. return RandomCode;
  127. }
  128. }
  129. }