shaideru
shaideru

Reputation: 45

IOS caseInsensitiveCompare

I have the code below that compares two values and returns if match is found, the problem is that it is case-sensitive, I googled around and found the method caseInsensitiveCompare, please help me run this program with caseInsensitiveCompare method, I am lost.

NSString *listOfnames = @"person, person1, person2, person3";
NSString *name = @"person2";

NSRange match = [listOfnames rangeOfString:name];

 if(match.location  == NSNotFound ){
    NSLog(@"Person not found!");
}else{
    NSLog(@"Found you");
}

Upvotes: 0

Views: 223

Answers (1)

Rob
Rob

Reputation: 437532

Use the rangeOfString:options: rendition with the NSCaseInsensitiveSearch option:

NSString *listOfnames = @"person, person1, person2, person3";
NSString *name = @"PERSON2";

NSRange match = [listOfnames rangeOfString:name options:NSCaseInsensitiveSearch];

if(match.location  == NSNotFound ){
    NSLog(@"Person not found!");
}else{
    NSLog(@"Found you");
}

Upvotes: 2

Related Questions