How to split a string into cocoa sentences

I have NSStringwith a few sentences, and I would like to break it down into NSArraysentences. Has anyone solved this problem before? I found enumerateSubstringsInRange:options:usingBlock:one that can do this, but it looks like it is not available on iPhone (Snow Leopard only). I was thinking about period-based line splitting, but that doesn't seem very reliable.

So far, my best option seems to have been to use RegexKitLite to regularly express it in an array of sentences. Solutions?

+3
source share
5 answers

CFStringTokenizer. kCFStringTokenizerUnitSentence.

+9

,

NSScanner *sherLock = [NSCanner scannerWithString:yourString]; // autoreleased
NSMutableArray *theArray = [NSMutableArray array]; // autoreleased
while( ![sherLock isAtEnd] ){
   NSString *sentence = @"";
   // . + a space, your sentences probably will have that, and you
   // could try scanning for a newline \n but iam not sure your sentences
   // are seperated by it
   [sherLock scanUpToString:@". " inToString:&sentence];
   [theArray addObject:sentence];
}

, , . NSScanner , .. , .

+3

, , NSString, NSCharacterSet NSScanner. , -[NSScanner scanUpToCharactersFromSet:intoString:]. , , .

, .

+1

:

NSArray *sentences = [string componentsSeparatedByString:@". "];

( "", "", "" ) ", , ".

0

NSArray * sentences = [asthmatic componentsSeparatedByCharactersInSet: [NSCharacterSet punctuationCharacterSet]];

0
source

Source: https://habr.com/ru/post/1730316/


All Articles