I'm just playing around with Xcode, and now using Xcode to find some string in another string. And problem here is that it doesn't show what I expect. (It shows "No match found!") Any tips, please? Thanks,

And my code is :

int main(int argc, const char * argv[])
{

    @autoreleasepool {



        NSString *listOfNames = @"huhuWARDdsfadfadsfsadfafsfsadfafr";
        NSString *capListOfNames = [listOfNames capitalizedString];

        NSString *name = @"WaRd";
        NSString *capName = [name capitalizedString];



        NSRange match = [capListOfNames rangeOfString:capName];
        if (match.location == NSNotFound)
        {
            NSLog(@"No match faound!");
        }else{
            NSLog(@"Found!");
            NSLog(@"location : %d",(int)match.location);
            NSLog(@"length : %d",(int)match.length);


        }


    }
    return 0;
}
有帮助吗?

解决方案

capitalizedString returns your string with an uppercase first letter and the rest lowercase.

Try again with capName = [name uppercaseString];

Currently, you're looking for "Ward" in a string that doesn't contain "Ward" (though it does contain "WARD").

EDIT: capitalizedString makes the first letter in each word of the string upper case, and the rest lower case.


Also, you can do this:

NSRange match = [capListOfNames rangeOfString:capName 
                                      options:NSCaseInsensitiveSearch];

Which, well, performs a case-insensitive search...

其他提示

capitalizedString only makes the first letter capitalized, use uppercaseString instead to achive what you expect.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top