@interface NSMutableString (extension)
-(void)quChuKongGe;
-(int)countNum;
@end
//去除首位的空格,并将最后一个字符改为*
//统计英文字符的个数
@implementation NSMutableString (extension)
-(void)quChuKongGe{
//去除首尾的空格
[self setString:[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
//将最后一个字符变为*
[self replaceCharactersInRange:NSMakeRange(self.length-1, 1) withString:@"*"];
}
-(int)countNum{
int count=0;
for (int i=0; i<self.length; i++) {
unichar ch=[self characterAtIndex:i];
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')){
count++;
}
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableString *str=[NSMutableString stringWithFormat:@" jjdhsggd "];
[str quChuKongGe];
NSLog(@"%@",str);
int num=[str countNum];
NSLog(@"%d",num);
}
return 0;
}
return count;
} |
|