字符串循环替换截取 《笔记篇》

感谢ios行业的同事。

方法一:

//  替换"<>"之间的字符串 为$1和$2之间的字符串    NSString *str=@"abcdeffed<12.\3>cba";

NSError* error = NULL;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"[^<]+(>)" options:0 error:&error];

NSString* result = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:@"$1$2"];

result=[result stringByReplacingOccurrencesOfString:@"<>" withString:@""];

NSLog(@"Result:%@", result);



方法二:

NSString *strrrrrrrrrr = @"abcdeffed<123>cba";

NSString *str2 = [[strrrrrrrrrr stringByReplacingOccurrencesOfString:@"<" withString:@"-"] stringByReplacingOccurrencesOfString:@">" withString:@"-"];

NSArray *arry = [str2 componentsSeparatedByString:@"-"];

NSMutableArray *newArray = [NSMutableArray array];

for (int i = 0; i < arry.count; i++) {

if (i%2 == 0) {

NSString *temp = arry[i];

if (temp.length > 0) {

[newArray addObject:arry[i]];

}

}

}

XPFLog(@" ====== %@", newArray);

NSString *string9 = [newArray componentsJoinedByString:@""];

NSLog(@"string9 : %@",string9);

你可能感兴趣的:(字符串循环替换截取 《笔记篇》)