Find similar strings without case sensitivity - iPhone

My application has a tableView and a search bar.

I have an NSMutable array to populate data in a tableView.

Now, whatever the custom types in the search string are - the data must be filtered appropriately and the tableView must be reloaded.

I have executed the following code in my application on the textField resignFirstResponder.

My question is inside this code.

-(BOOL)textFieldShouldReturn:(UITextField*)txt
{
      [txt resignFirstResponder];
      // on textfield resign searchData will be called 
      [self searchData];
      return YES;
}
-(void)searchData
{
       // N -> total elements & i for loop
       NSInteger n=[CategoryDtlArray count],i;
       //CategoryDtl is my custom class & it objects are stored in my array
       CategoryDtl *tmpCat;
       // dtl string -> which is needed for comparison
       NSString *dtl;
       for (i=0; i<n; i++)
       {
               // got the object from array
               tmpCat=(CategoryDtl*)[CategoryDtlArray objectAtIndex:i];
               // got the description from the object for comparison  
               dtl=[NSString stringWithString:tmpCat.Tip_Description];
               // string to search
               NSString *strSrch=[NSString stringWithString:txtSearch.text];
               // now I don't know how to check my object String
               // is starting with search string or not?
               // if it starts with search string -> it should be displayed in table
               // else not.
               // "How to implement this?"
               // String comparison without case sensitivity
       }
}

      

Thanks in advance for your help.

+2


source to share


2 answers


Does it help?



if ([dtl hasPrefix:strSrch])  
{  
    //  match!
}  

      

+2


source


See NSArray -filteredArrayUsingPredicate :. You will pass this NSPredicate object, which compares the properties of string array objects to a string property using case insensitive / diacritical comparison.



0


source







All Articles