iOS学习开发iOS技术分享

iOS 字符串相邻字符去重

2018-04-03  本文已影响25人  Fire_day
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

上一篇 下一篇

猜你喜欢

热点阅读