string str = "akdjkaflajfaljfqpjwojhflkjallaajfjadjlajdaffksbvcalihwhkqekq12304010580473639601"; int sum = str.Length; Console.WriteLine(sum);//计算字符串的长度,sum=80,可以不输出sum的结果. Random r = new Random(); //先从0到79中随机生成一个数。 int i1 = r.Next(79); int i2 = r.Next(79); int i3 = r.Next(79); int i4 = r.Next(79); //从0到79生成的随机数中找到字符串对应的索引并进行截取。 string s1 = str.Substring(i1, 1); string s2 = str.Substring(i2, 1); string s3 = str.Substring(i3, 1); string s4 = str.Substring(i4, 1); Console.WriteLine("生成的4位验证码为{0}{1}{2}{3}",s1,s2,s3,s4); Console.ReadLine();
时间: 2024-10-25 20:12:54