如何计算 NSString 中大写字符的数量?
我试图找到计算 NSString 中大写字符数量的最佳方法。我知道如何使用以下代码找出某个字符是否为大写:
NSString *s = @"This is a string";
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];
What would be the best way to count the number of uppercase letter in a NSString?谢谢。
I'm trying to find out the best way to count the number of uppercase characters that are in a NSString. I know how to find out if a certain character is uppercase by using this code:
NSString *s = @"This is a string";
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];
What would be the best way to count the number of uppercase letters in a NSString? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
count
是大写字母出现的次数。count
is the number of uppercase occurences.逐一遍历字符串中的索引,只要找到大写字母,就将
1
添加到计数器。Walk through the indices in the string one by one and add
1
to a counter whenever you find an uppercase letter.一条线解决方案
One line solution