objective-ccocoansstringsubstring

Objective-C String Contains String Method


How can I check if an NSString contains another substring, at which point it will return a Boolean value?

This is what I'm thinking of:

if myString.contains("string") then
{
    // Stuff Happens
}

But, from the research I've done, it seems as if Obj-C has no built-in method for this. This Wikipedia article gives numerous string methods, their differences, and all in different languages, but I see no Obj-C support for anything like a "contains" method.

Does anyone know of a simple-to-use method like the one above (which is similar to the C# and VB.NET function)?

Would a "find" method work? If so, how?


Solution

  • if ([myString rangeOfString:@"string"].location != NSNotFound)
    {
        // Stuff happens
    }