检测字符串是否全部为CAPS [英] Detecting if a string is all CAPS
本文介绍了检测字符串是否全部为CAPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中,有没有一种方法可以检测字符串是否全部大写?
In C# is there a way to detect if a string is all caps?
大多数字符串都较短(即100个字符以下)
Most of the strings will be short(ie under 100 characters)
推荐答案
无需创建新字符串:
bool IsAllUpper(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (!Char.IsUpper(input[i]))
return false;
}
return true;
}
编辑:如果要跳过非字母字符( OP的原始实现没有,但他/她的注释表明他们可能想要):
If you want to skip non-alphabetic characters (The OP's original implementation does not, but his/her comments indicate that they might want to) :
bool IsAllUpper(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Char.IsLetter(input[i]) && !Char.IsUpper(input[i]))
return false;
}
return true;
}
这篇关于检测字符串是否全部为CAPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文