NSString:一个单词的数字

Pie*_*rre 1 cocoa objective-c nsstring

可能重复:
NSString中子字符串的出现次数?

我想知道是否有一个返回给定单词出现的方法.


NSString *string = @"ok no ok no no no ok";
// How to return 4 for the word no ?
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助 !

Man*_*ath 5

使用此API: - (NSArray*)componentsSeparatedByString:(NSString*)分隔符

NSString* stringToSearch = @"ok no ok no no no ok";
NSString* stringToFind = @"no";
NSArray *listItems = [list componentsSeparatedByString:stringToFind];
int count = [listItems count] - 1;
Run Code Online (Sandbox Code Playgroud)

您将在"count"变量中获得字符串@"no"的出现次数