如:aaabbs[e]yuejiashidi[/e]suehahs[e]ueha[/e]lsuehahs[e] [/e]
则输出:yuejiashidi
ueha
ueha
//装载每一对[e][/e]之间的字符串
NSMutableArray *subArray = [NSMutableArraynew];
//表示当前这对[e][/e]的字符串,如[e]yuejiashidi[/e],则它表示为yuejiashidi
NSMutableString *subString = [NSMutableStringnew];
//原字符串
NSMutableString *str = [[NSMutableStringalloc] initWithFormat:@"%@", @"aaabbs[e]yuejiashidi[/e]suehahs[e]ueha[/e]lsuehahs[e]ueha[/e]"];
//遍历这条原字符串
for (int i = 0; i < str.length; i ++) {
char ch = [str characterAtIndex:i];
//如果这条字符串是以[e]开头
if ([str hasPrefix:@"[e]"]) {
char subCh = [str characterAtIndex:i + 3]; //得到[e]后的字符,
if (subCh == '[') {
char nextCh = [str characterAtIndex:i + 1 + 3];
if (nextCh == '/') {
NSString *str1 = [NSString stringWithString:subString];
[subArray addObject:str1];
[str deleteCharactersInRange:NSMakeRange(0, i + 3 + 4)];
i = -1;
[subString setString:@""];
}else{
[subString appendFormat:@"%c", subCh];
}
}else{
[subString appendFormat:@"%c", subCh];
}
}else{
if (ch == '[') {
[str deleteCharactersInRange:NSMakeRange(0, i)];
i = -1;
}
}
}
for (NSString *sub in subArray) {
NSLog(@"=sub %@",sub);
}