需求:以。或者!或者?分割字符串
思路一:
NSString *testStr = @"我的世界观。我的美女?我的老板!哈哈哈。";
NSString *pageStr = [self stringOfPage:index];
NSMutableArray*synthesisTexts = [NSMutableArrayarray];
NSArray*synthesisTemp = [testStr componentsSeparatedByString:@"。"];
在遍历NSArray数组分别以?!分割
思路二:采用正则表达式分割
NSString *testStr = @"我的世界观。我的美女?我的老板!哈哈哈。";
__block NSRange lastRange = NSMakeRange(-1, 0);
NSError *error = NULL;
NSRegularExpression *fieldRegularExpression = [NSRegularExpression
regularExpressionWithPattern:@"[。?!]"
options:NSRegularExpressionCaseInsensitive
error:&error];
[fieldRegularExpression enumerateMatchesInString:testStr options:0 range:NSMakeRange(0, [testStr length])
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
NSString *splitStr = [testStr substringWithRange:NSMakeRange(lastRange.location + 1, match.range.location - lastRange.location)];
NSLog(@"%@---%ld",splitStr,match.range.location+1);
lastRange = match.range;
}];
}
网友评论