我有一个 iOS 应用程序,它有一个搜索栏和一个 UITableView。当我点击搜索栏(例如“ta”)时,网络服务会向我返回所有包含“at”的单词, 例如“beta”,“mota”,“at work”,“ebebebatbcbcb”,我只想包含以“at”开头的单词,而不是所有包含“at”的单词。
感谢您的回答。
最佳答案
试试这个:
-(NSMutableArray *)array:(NSMutableArray *)array withstart:(NSString *)string{
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",string];
[array filterUsingPredicate:sPredicate];
return array;
}
或者其他方式:
NSString *prefix = @"at";
NSArray *final_array=[array objectsAtIndexes:[array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop)
{
return [obj hasPrefix:prefix];
}]];
关于iphone - 对 NSString 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530494/