How to see if a string contains a substring in objective-c?

How to see if a string contains a substring in objective-c?


Posted in : Java Posted on : April 27, 2015 at 5:33 PM Comments : [ 0 ]

How to check occurrence of a string in a big string and return if it is present or not?

Example of finding a substring in a String in Objective C

If you are working on any iOS project then at some point of time you may have to find the occurrence of a substring in a big string. So, at that time you can use the logic given here.

This video tutorial explains you how to find the existence of a substring in a big string.

Suppose you have a string with a lot of text e.g.:

NSString *strSourceString = myfield.text;

An you want to find if it contains a small part of string. Then you can use the following code:

- (BOOL) ifContainsString: (NSString*) substring source:(NSString *)sourceString{
    return [sourceString rangeOfString:substring].location != NSNotFound;
}

Source: Roseindia.net iOS Tutorial.

So, This method can be used for checking the presence of a substring.

The method rangeOfString is of the NSString class of iOS platform.

This method of NSString class is used to find and return the range of the first occurrence of the supplied string within the current string. Above function is using this method of NSString class.

Here is the video tutorial:

Following is the declaration of the method in SWIFT:

func rangeOfString(_ aString: String) -> NSRange

The method declaration in OBJECTIVE-C is:

- (NSRange)rangeOfString:(NSString *)aString

Function takes on parameter of type NSString and then search for that in the source string.

Function also raises the NSInvalidArgumentException if string passed as parameter is nil.

Check more tutorials at Devmanuals.com IOS Tutorials.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics