//去除字符串內的相鄰的重復字符,例如:aabdaaadesff,最後結果abdadesf
NSMutableString *string = [NSMutableString stringWithFormat:@"aabbssddffiieeoo"];
NSString *strIndex1 = @"";
NSString *strIndex2 = @"";
for (NSInteger i = 0; i <= [string length]; i++) {
strIndex1 = [string substringWithRange:NSMakeRange(i, 1)];
strIndex2 = [string substringWithRange:NSMakeRange(i + 1, 1)];
if ([strIndex1 isEqualTo:strIndex2]) {
[string deleteCharactersInRange:NSMakeRange(i + 1, 1)];
}
}
NSLog(@"%@", string);
}
運行出來有錯誤 希望大神幫看看
NSMutableString *string = [NSMutableString stringWithFormat:@"aabbbssddffffiieeoo"];
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;
}
}
}