字符串压缩


网上看到的一个帖子,保存下来,

-(NSString *)simplifyStringFromString:(NSString *)str{ 

    int strIndex = 0;  
    int repeatCount = 0;  
    NSString *tempStr = nil;  
    NSString *returnStr = nil;  
    while (strIndex <= str.length) {  
        NSString *c = nil;  
        if (strIndex != str.length) {  
            c = [str substringWithRange:NSMakeRange(strIndex, 1)];  
        }  
        if (strIndex == 0) {  
            tempStr = c;  
        }  
        if (![c isEqualToString:tempStr]) {  
            if (repeatCount != 0) {  
                returnStr = [NSString stringWithFormat:@"%@%d",returnStr ? returnStr : @"",repeatCount];  
            }  
            returnStr = [NSString stringWithFormat:@"%@%@",returnStr ? returnStr : @"",tempStr];  
            tempStr = c;  
            repeatCount = 1;  
        }else{  
            repeatCount ++;  
        }  
        strIndex ++;  
    }  
    return returnStr;  
}  
 
    NSString *str = @"abbcccddddeeeeeffffff";  
    NSString *result = [self simplifyStringFromString:str];  
    NSLog(@"%@",result);  
 
log 1a2b3c4d5e6f 

你可能感兴趣的:(ios,Objective-C,iPhone,ipad,Object_C)