0

我需要用特定的标点符号分割一个句子,但要保留这些标点符号。

我这样做是这样的:

NSCharacterSet *delimeter = [NSCharacterSet characterSetWithCharactersInString:@"***"];

sentence  = [sentence  stringByReplacingOccurrencesOfString:@"," withString:@",***"];
sentence  = [sentence  stringByReplacingOccurrencesOfString:@"!" withString:@"!***"];
sentence  = [sentence  stringByReplacingOccurrencesOfString:@"?" withString:@"?***"];
sentence  = [sentence  stringByReplacingOccurrencesOfString:@"." withString:@".***"];
sentence  = [sentence  stringByReplacingOccurrencesOfString:@":" withString:@":***"];
sentence  = [sentence  stringByReplacingOccurrencesOfString:@";" withString:@";***"];

NSArray *sentenceArray = [sentence  componentsSeparatedByCharactersInSet: delimeter];
NSMutableArray *finalSentenceArray = [sentenceArray mutableCopy];
[finalSentenceArray removeObject:@""];

但是这样做我有需要删除的空字符串。什么是更有效的方法?

谢谢!

4

0 回答 0