获取以特定单词开头的txt文件名称

问题描述:

参考此问题 Get all file names starting with a prefix from Resource folder 这里它表示单词'前缀'。但在我的应用程序中,我需要从文本字段中给出的单词开始的所有文件名称到表格中。我怎样才能实现呢?我尝试了“%@”,然后提供了textfield内容。但它不工作。但是,如果我给“h”之类的东西,它会显示所有以'h'开头的文件名。请帮忙回复。获取以特定单词开头的txt文件名称

-(IBAction)Do_Search:(id)sender 
{  
    files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: 
      [[NSBundle mainBundle] bundlePath] error:nil]; 
    search_results_array = [files filteredArrayUsingPredicate: 
          [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'h'"]]; 
    NSLog(@"%@", search_results_array); 
    int search_res_count=[search_results_array count]; 
    NSLog(@"Array has %d results...",search_res_count); 
    [search_results_array retain]; 
    [Result_table reloadData]; 
    //till this part it works fine. I got all file names starting with 'h' in table. now what i need is i should get all file names starting with the word given in the search bok 

    NSString *Search_string=Search_song.text; 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:Search_string ofType:@"txt"]; 
    if (filePath) { 
     NSString *myText = [NSString stringWithContentsOfFile:filePath]; 
     if (myText) { 
      song_lyrics.text= myText; 
      //what i did here is if i give a complete file name in the search field (textfield). i am getting the complete file contents inside a text view 
     } 
    } 
} 

我知道代码现在没有正确的顺序。我试图用这种自上而下和自下而上的方法..

+1

你可以给你的代码,你试图实现这个?给出了代码 – RaysonK 2012-01-31 06:25:31

+0

。抱歉,我试图将数据加载到表格中。 – priya 2012-02-01 07:00:19

它是如此简单....代替 'H' 我删除了从那里 '',并给%@即

search_results_array = [files filteredArrayUsingPredicate: 
         [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@",Search_string]]; 

那里解决了我的问题...谢谢大家:)

+0

请务必接受您自己的回答 – RaysonK 2012-02-01 18:54:56

+0

我确定...我得到了结果... – priya 2012-02-02 03:45:00

+1

我的意思是点击投票数 – RaysonK 2012-02-02 04:25:03

Pl。改变线

search_results_array = [files filteredArrayUsingPredicate: 
         [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'h'"]]; 

如下

search_results_array = [files filteredArrayUsingPredicate: 
         [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] '%@'",searchBoxText]]; 
+0

该数组是空的然后在这里searchBoxText是Search_song为我。 – priya 2012-02-01 08:04:02