How to check if a string contains another string in Objective-C?

Posted by Ramesh Singh | Updated on

If you want to check if a string contains another string in Objective-C? Use the below example:

The key is noticing that rangeOfString: returns an NSRange struct, and the documentation says that it returns the struct {NSNotFound, 0} if the “haystack” does not contain the “needle”.

And if you’re on iOS 8 or OS X Yosemite, you can now do: (*NOTE: This WILL crash your app if this code is called on an iOS7 device).


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers