gpt4 book ai didi

objective-c - NSString : number of a word

转载 作者:行者123 更新时间:2023-12-03 16:49:29 26 4
gpt4 key购买 nike

Possible Duplicate:
Number of occurrences of a substring in an NSString?

我想知道是否有一种方法可以返回给定单词的出现次数。


NSString *string = @"ok no ok no no no ok";
// How to return 4 for the word no ?

感谢您的帮助!

最佳答案

使用此 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;

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

关于objective-c - NSString : number of a word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2370574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com