iOS 字符串相邻字符去重

NSMutableString *string = [NSMutableString stringWithString:@"aabacccbbaa"];
    
for (NSInteger i = 0; i < string.length - 1; i++) {
    
    for (NSInteger j = i+1; j < string.length; j++) {
        
        NSString *str1 = [string substringWithRange:NSMakeRange(i, 1)];
        NSString *str2 = [string substringWithRange:NSMakeRange(j, 1)];
        if ([str1 isEqualToString:str2]) {
            [string deleteCharactersInRange:NSMakeRange(j, 1)];
            j--;
        } else {
            break;
        }
        
    }
    
}
NSLog(@"%@", string);

输出 abacba

你可能感兴趣的:(iOS 字符串相邻字符去重)