[英]How to check characters at a certain position in a string .net
In my project the customer have a card and a 7 letter particular security code is in it. 在我的项目中,客户有一张卡,里面有一个7字母的特定安全码。 I want to ask 3 letters from that security code by position. 我想按位置询问该安全代码中的3个字母。
eg. card security code is **57GHY58**
I want to ask what is the Character at 2,4 and 7 position in your security code ? 我想问一下您的安全代码在2,4和7位的字符是什么 ?
answer is **7H8**
How to generate that question with random postion and how to check it ? 如何生成带有随机位置的问题并进行检查 ?
private static int[] GetThreeRandomNumbers()
{
List<int> list = new List<int>();
Random r = new Random();
while (list.Count < 3)
{
int num = r.Next(1, 7);
if (!list.Contains(num))
{
list.Add(num);
}
}
list.Sort();
return list.ToArray();
}
You have a string with indices 0-6. 您有一个索引为0-6的字符串。 You need to pick 3 indices from that range randomly. 您需要从该范围中随机选择3个索引。 The Random
class will help you with this, have a look at its method Random.Next(int, int)
, it will return you a random number from the specified range. Random
类将帮助您解决此问题,请查看其方法Random.Next(int, int)
,它将为您返回指定范围内的随机数。 Then the only other thing you have to do is skip the indices you have already used. 然后,您要做的唯一另一件事就是跳过已经使用的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.