iphonexcodensstringstring-comparisontext-comparison

How to test a string for text


I would like to test a string to see if anywhere it contains the text "hello". I would like the test to not take into account capitalization. How can I test this string?


Solution

  • Use the below code as reference to find check for a substring into a string.

        NSString* string = @"How to test a string for text" ;
        NSString* substring  = @"string for" ;
    
        NSRange textRange;
        textRange =[string rangeOfString:substring  options:NSCaseInsensitiveSearch];
    
        if(textRange.location != NSNotFound)
        {
    
        //Does contain the substring
        }