在objective c中字符串數組中的字符串搜索。

[英]String search in string array in objective c


I want to search a specific string in the array of strings in objective c. Can somebody help me in this regard?

我想在objective c的字符串數組中搜索一個特定的字符串,有人能幫我一下嗎?

3 个解决方案

#1


173  

BOOL isTheObjectThere = [myArray containsObject: @"my string"];

or if you need to know where it is

或者你需要知道它在哪里。

NSUInteger indexOfTheObject = [myArray indexOfObject: @"my string"];

I strongly recommend you read the documentation on NSArray. It's best to do that before posting your question :-)

我強烈建議您閱讀NSArray的文檔。最好在發布你的問題之前這么做:-)

#2


43  

You can use NSPredicate class for searching strings in array of strings. See the below sample code.

可以使用NSPredicate類搜索字符串數組中的字符串。請參見下面的示例代碼。

NSMutableArray *cars = [NSMutableArray arrayWithObjects:@"Maruthi",@"Hyundai", @"Ford", @"Benz", @"BMW",@"Toyota",nil];

NSString *stringToSearch = @"i";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",stringToSearch]; // if you need case sensitive search avoid '[c]' in the predicate

NSArray *results = [cars filteredArrayUsingPredicate:predicate];

This is the most efficient way for searching strings in array of strings

這是在字符串數組中搜索字符串最有效的方法。

#3


2  

NSMutableArray *cars = [NSMutableArray arrayWithObjects:@"Max",@"Hai", @"Fine", @"Bow", @"Bomb",@"Toy",nil];

NSString *searchText = @"i";
 NSArray *results = [cars filteredArrayUsingPredicate:predicate];

// if you need case sensitive search avoid '[c]' in the predicate

 NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"title contains[c] %@",
                                searchText];


searchResults = [cars  filteredArrayUsingPredicate:resultPredicate];

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/05/10/725479cca982d2b8f186c647dd89da5a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com