检测一个字符串是否都是CAPS(Detecting if a string is all CAPS)
在C#中有一种方法可以检测字符串是否全部大写?
大部分字符串都会很短(即少于100个字符)
In C# is there a way to detect if a string is all caps?
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的原始实现不,但他/她的评论表明他们可能想要 ):
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; }No need to create a new string:
bool IsAllUpper(string input) { for (int i = 0; i < input.Length; i++) { if (!Char.IsUpper(input[i])) return false; } return true; }Edit: 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; }更多推荐
发布评论