#import <Foundation/Foundation.h>
@interface NSString( MJ )// 方法声明
+(int)letterCount: (NSMutableString *)s1;
+(NSMutableString *)qukongge:(NSMutableString *)s1;
@end
@implementation NSString( MJ )// 方法实现
+(int)letterCount: (NSMutableString *)s1
{
int count = 0;
for (int i=0 ;i<self.length;i++)
{
unichar c = [self characterAtIndex :i];
if(( c>='65'&& c<='90')||(c>='97'&& c<='122'))
{
count++;
}
}
return count;
}
+(NSMutableString *)qukongge:(NSString *)s1
{
NSMutableString *newS1 = [NSMutableString stringWithString:s1];
NSMutableString *temp = nil ;
for(int i =0; i < self.length; i++)
{
char c = [newS1 characterAtIndex:i]; // 获取当前位置的字符
if(c == ' ')
{
temp = [newS1 deleteCharactersInRange:NSMakeRange(i, 1)];
}
else break;
}
for(int k = self.length -1; k >= 0; k--)
{
char c2 = [newS1 characterAtIndex:i]; // 获取当前位置的字符
if(c2 == ' ')
{
temp = [newS1 deleteCharactersInRange:NSMakeRange(k, 1)];
}
else break;
}
return temp;
}
@end
int main ()
{ NSMutableString *s1 = @" 12aaabbb35 45 ";
NSMutableString *s2 = [NSString qukongge: s1];
int count = [NSString letterCount: s1] ;
NSLog(@"去空格后是 %@,字母个数是%@", s2,count);
return 0;
}
|