"之间的字符串 为$1和$2之间的字符串 NSString ...">
美文网首页NSStringSO 问题来了
字符串循环替换截取 《笔记篇》

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

作者: 失忆的程序员 | 来源:发表于2017-04-20 17:28 被阅读15次

感谢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);

相关文章

网友评论

    本文标题:字符串循环替换截取 《笔记篇》

    本文链接:https://www.haomeiwen.com/subject/zrblzttx.html