检测一个字符串是否都是CAPS(Detecting if a string is all CAPS)

编程入门 行业动态 更新时间:2024-10-26 23:40:05
检测一个字符串是否都是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; }

更多推荐

本文发布于:2023-04-29 23:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336699.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都是   字符串   CAPS   Detecting   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!