本帖最后由 15098037606 于 2015-4-20 19:10 编辑
#import <Foundation/Foundation.h> @interface NSString( XZ )// 方法声明 + (void) zifuchuanfanzhuan: (NSMutableString *)s1; + (int) letterCount: (NSMutableString *)s2; + (NSMutableString *) qukongge: (NSMutableString *)s3; @end
@implementation NSString( XZ )// 方法实现
+ (void) zifuchuanfanzhuan: (NSMutableString *)s1 { int changDu = (int)s1.length;
// 将字符串最右端字符的下标储存在i中,遍历,直到字符串中间位置结束 for (int i = (changDu - 1); i >= (changDu / 2); i--) { // 在遍历的同时储存要交换的两个字符的值 char right = [s1 characterAtIndex:i]; char left = [s1 characterAtIndex: (changDu - i - 1)];
// 交换两个字符的值,同时将两个字符转换成字符串,方便后面的替换字符串操作 NSString *rightNow = [[NSString alloc] initWithFormat: @"%c",left]; NSString *leftNow = [[NSString alloc]initWithFormat: @"%c",right];
// 将交换好的字符的字符串替换进原来的字符串中 [s1 replaceCharactersInRange:NSMakeRange(i, 1) withString:rightNow]; [s1 replaceCharactersInRange:NSMakeRange(changDu - i - 1, 1) withString:leftNow]; }
+ (int) letterCount: (NSMutableString *)s2 { int count = 0; for (int i=0 ;i<self.length;i++) { unichar c = [self characterAtIndex :i]; if(( c>='a'&& c<='z')||(c>='A'&& c<='Z'))//判断是否为英文字母 { count++; } } return count; }
+ (NSMutableString *) qukongge: (NSString *)s3 { NSMutableString *newS1 = [NSMutableString stringWithString:s3]; 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; }
return temp; }
@end
int main() { NSMutableString *s1 = @" 123 "; NSMutableString *s2 = @" 5435abc54abc3AHJ5 "; NSMutableString *s3=@" 1235 45 ";
NSMutableString *s1 = [NSString zifuchuanfanzhuan:s1] int count = [NSString letterCount: s2] ; NSMutableString *s3 = [NSString qukongge: s3]; NSLog(@"字符串反转后输出是%@", s1); NSLog(@"用类方法2后,字母个数是%d", count); NSLog(@"用类方法3去两端空格后,输出是 %@", s3); return 0; }
警告:method for definition 'qukongge:' not found method for definition 'letterCount:' not found 报错:use of declared identifier 'letterCount' 哪位大神帮我修改一下?
|