把字符串中的字母大小写反转OC

//将s中每个字母大写变成小写,小写变成大写,其他字符不动。返回值是修改后的字符串

-(NSString *)reversalUpperAndLowerForString:(NSString *)s
{
    NSMutableString *str=[[NSMutableString alloc]init];
    for (int i=0; i<s.length; i++) {
        NSString *subStr=[s substringWithRange:NSMakeRange(i, 1)];
        unichar j=[subStr characterAtIndex:0];
        if ((j>='a')&&(j<='z')) {
            [str appendString: [subStr uppercaseString]];
        }
        else if((j>='A')&&(j<='Z'))
        {
           [str appendString: [subStr lowercaseString]];
        }
        else
        {
            [str appendString:subStr];
        }
        
    }
    return str;
}
+(void)test
{
    NSString *str=@"hello123WORD";
    Reight *eig=[[Reight alloc]init];
    NSString *substr= [eig reversalUpperAndLowerForString:str];
    NSLog(@"%@",substr);
}

posted @ 2015-06-19 09:06  天使雨儿  阅读(1249)  评论(0编辑  收藏  举报