anasabufarraj anasabufarraj - 5 months ago 9
iOS Question

How to get an NSString from another in a case-insensitive NSString method?

How to get an NSString from

*listOfNames
that matches the NSString
*keyword
in a case-insensitive NSString method:

NSString *listOfNames = @"Anas, Ward, Qusai, Zienab";
NSString *keyword = @"ward";


So..here the output should be capitalised
// Ward
from
*listOfNames

Answer

Here ya go buddy

    NSString *listOfNames = @"Anas, Ward, Qusai, Zienab";
    NSString *keyword = @"ward";
    NSArray *namesArray = [listOfNames componentsSeparatedByString:@", "];

    NSString *foundStr;

    for (int i=0; i<[namesArray count]; i++)
    {
        NSString *stringFromArray = [namesArray objectAtIndex:i];

        //now we gonna compare string by string
        if ([stringFromArray rangeOfString:keyword options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            foundStr = stringFromArray;
            NSLog(@"foundStr: %@ ...", foundStr);
            break; //found it!
        }
    }

2016-06-23 16:37:05.825 014-test-proj[4502:83719] foundStr: Ward ...