使用大写的索引检查字符串内容

时间:2014-04-13 17:15:18

标签: c#

我想检查字符串是否包含总共31个字符,索引0,1,30是否为大写。例如:

string MyTestString = "INbwzfnpdmcwqq1dl22mcsdeycet2dD";
If (MyTestString.Contains(31 characters and index 0,1,30 are uppercase))
{
// proceed with rest of program
}

2 个答案:

答案 0 :(得分:3)

if (MyTestString.Length ==31 && Char.IsUpper(MyTestString[0]) && Char.IsUpper(MyTestString[1]) && Char.IsUpper(MyTestString[30]))
{
}

答案 1 :(得分:3)

if (str.Length == 31 && new []{ str[0], str[1], str[30] }.All(char.IsUpper))
  

31个字符,索引1,2,31为大写

如果你有一个长度为31的字符串,那么最后一个索引是30,因为数组索引是基于零的,因此从0开始到length - 1。来自specs

  

对于长度为N的维度,索引的范围可以从0到N-1(含) -