C#自定义的字符串操作增强类

 1 using System;

 2 

 3 namespace DotNet.Utilities

 4 {

 5     public class RandomOperate

 6     {

 7         // 一:随机生成不重复数字字符串  

 8         private int rep = 0;

 9         public string GenerateCheckCodeNum(int codeCount)

10         {

11             string str = string.Empty;

12             long num2 = DateTime.Now.Ticks + this.rep;

13             this.rep++;

14             Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));

15             for (int i = 0; i < codeCount; i++)

16             {

17                 int num = random.Next();

18                 str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();

19             }

20             return str;

21         }

22 

23         //方法二:随机生成字符串(数字和字母混和)

24         public string GenerateCheckCode(int codeCount)

25         {

26             string str = string.Empty;

27             long num2 = DateTime.Now.Ticks + this.rep;

28             this.rep++;

29             Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));

30             for (int i = 0; i < codeCount; i++)

31             {

32                 char ch;

33                 int num = random.Next();

34                 if ((num % 2) == 0)

35                 {

36                     ch = (char)(0x30 + ((ushort)(num % 10)));

37                 }

38                 else

39                 {

40                     ch = (char)(0x41 + ((ushort)(num % 0x1a)));

41                 }

42                 str = str + ch.ToString();

43             }

44             return str;

45         }

46 

47         #region

48 

49         /// <summary>

50         /// 从字符串里随机得到,规定个数的字符串.

51         /// </summary>

52         /// <param name="allChar"></param>

53         /// <param name="CodeCount"></param>

54         /// <returns></returns>

55         private string GetRandomCode(string allChar, int CodeCount)

56         {

57             //string allChar = "1,2,3,4,5,6,7,8,9,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"; 

58             string[] allCharArray = allChar.Split(',');

59             string RandomCode = "";

60             int temp = -1;

61             Random rand = new Random();

62             for (int i = 0; i < CodeCount; i++)

63             {

64                 if (temp != -1)

65                 {

66                     rand = new Random(temp * i * ((int)DateTime.Now.Ticks));

67                 }

68 

69                 int t = rand.Next(allCharArray.Length - 1);

70 

71                 while (temp == t)

72                 {

73                     t = rand.Next(allCharArray.Length - 1);

74                 }

75 

76                 temp = t;

77                 RandomCode += allCharArray[t];

78             }

79             return RandomCode;

80         }

81 

82         #endregion

83     }

84 }

85 

86 //该代码片段来自于: http://www.sharejs.com/codes/csharp/8646
View Code

 

你可能感兴趣的:(字符串)