How to split a NSString keeping specific punctuations?

45 Views Asked by At

I need to split a sentence in specific punctuation marks, but keeping these punctuations.

I'm doing it like this:

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:@""];

But doing like this I'm having empty strings that need to be removed. What is a more efficient way of doing it?

Thanks!

0

There are 0 best solutions below